An Introduction to STL.

Slides:



Advertisements
Similar presentations
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Advertisements

C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Algorithms Series of mathematical or variable manipulations Integer a,b,c a = 12 b = 22 c = a * b (what is c?) 12 * 22 = 264.
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
C++ crash course Class 3 designing C++ programs, classes, control structures.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Object Oriented Programming Elhanan Borenstein Lecture #11 copyrights © Elhanan Borenstein.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
1 C++ Syntax and Semantics, and the Program Development Process.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
An Introduction to STL. The C++ Standard Template Libraries  In 1990, Alex Stepanov and Meng Lee of Hewlett Packard Laboratories extended C++ with a.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
Standard C++ Library Part II. Last Time b String abstraction b Containers - vector.
Lecture 7 : Intro. to STL (Standard Template Library)
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
Module 5: I/O and Strings #1 2000/01Scientific Computing in OOCourse code 3C59 Module 5: I/O and STRINGS In this module we will cover The C++ input and.
Lecture 36 OOP The STL Standard Template Library
CS212: Object Oriented Analysis and Design
“Generic Programming” ECE 297
Introduction to olympic programming
Strings: C-strings vs. Strings as Objects
Pointers and Linked Lists
Pointers and Linked Lists
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Lecture 7-2 : STL Iterators
C++ Standard Library.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Standard Template Library (STL)
Introduction to C++ October 2, 2017.
Generic Programming Techniques in C++
Starting Out with C++ Early Objects Eighth Edition
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Lecture 7-3 : STL Algorithms
C++ Templates L03 - Iterator 10 – Iterator.
10 – Iterators C++ Templates 4.6 The Iterator pgs
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Lecture 7-2 : STL Iterators
Engineering 1020: Introduction to Programming Fall 2018
Today’s Learning Objective
Popping Items Off a Stack Lesson xx
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Strings: C-strings vs. Strings as Objects
More About Data Types & Functions
C++ File Structure and Intro to the STL
Doubly Linked List Implementation
Engineering Problem Solving with C++, Etter
Iterators and STL Containers
Lecture 8 : Intro. to STL (Standard Template Library)
Standard Template Library (STL)
Lecture 8-2 : STL Iterators and Algorithms
C++ Templates L03 - Iterator 10 – Iterator.
C++ File Structure and Intro to the STL
Today’s Objectives 28-Jun-2006 Announcements
Programming Strings.
Collections Intro What is the STL? Templates, collections, & iterators
Standard Template Library
Standard C++ Library Part II.
Chapter 3 Lists, Stacks, and Queues
Presentation transcript:

An Introduction to STL

The C++ Standard Libraries Usually referred to as STL. Input / Output String abstraction Containers Algorithms

Strings In C we used char * to represent a string. The C++ standard library provides a common implementation of a string class abstraction named string.

Hello World - C #include <stdio.h> void main() { // create string ‘str’ = “Hello world!” char *str = “Hello World!”; printf(“%s\n”, str); }

Hello World – C++ #include <iostream> #include <string> using namespace std; int main() { // create string ‘str’ = “Hello world!” string str = “Hello World!”; cout << str << endl; return 0; }

String To use the string type simply include its header file. #include <string>

Creating strings string str = “some text”; or string str(“some text”); other ways: string s1(7,‘a’); string s2 = s1;

string length The length of string is returned by its size() operation. #include <string> string str = “something”; cout << “The size of “ << str << “is “ << str.size() << “characters.” << endl;

The size method str.size() ??? In C we had structs containing only data, In C++, we have : class string { … public: unsigned int size(); };

String concatenation concatenating one string to another is done by the ‘+’ operator. string str1 = “Here ”; string str2 = “comes the sun”; string concat_str = str1 + str2;

String comparison To check if two strings are equal use the ‘==‘ operator. string str1 = “Here ”; string str2 = “comes the sun”; if ( str1 == str2 ) /* do something */ else /* do something else */

String assignment To assign one string to another use the “=“ operator. string str1 = “Sgt. Pappers”; string str2 = “lonely hearts club bend”; str2 = str1; Now : str2 equals “Sgt. Pappers”

What more ? Containers Algorithms

Containers Data structures that hold anything (other objects). list: doubly linked list. vector: similar to a C array, but dynamic. map: set of ordered key/value pairs. Set: set of ordered keys.

Algorithms generic functions that handle common tasks such as searching, sorting, comparing, and editing: find merge reverse sort and more: count, random shuffle, remove, Nth-element, rotate.

Vector Provides an alternative to the built in array. A vector is self grown. Use It instead of the built in array!

Defining a new vector Syntax: vector<of what> For example : vector<int> - vector of integers. vector<string> - vector of strings. vector<int * > - vector of pointers to integers. vector<Shape> - vector of Shape objects. Shape is a user defined class.

Using Vector #include <vector> Two ways to use the vector type: Array style. STL style

Using a Vector – Array Style We mimic the use of built-in array. void simple_example() { const int N = 10; vector<int> ivec(N); for (int i=0; i < 10; ++i) cin >> ivec[i]; int ia[N]; for ( int j = 0; j < N; ++j) ia[j] = ivec[j]; }

Using a vector – STL style We define an empty vector vector<string> svec; we insert elements into the vector using the method push_back. string word; while ( cin >> word ) //the number of words is unlimited. { svec.push_back(word); }

Insertion void push_back(const T& x); Inserts an element with value x at the end of the controlled sequence. svec.push_back(str);

Size unsigned int size(); Returns the length of the controlled sequence (how many items it contains). unsigned int size = svec.size();

Class Exercise 1 Write a program that read integers from the user, sorts them, and print the result.

Solving the problem Easy way to read input. A “place” to store the input A way to sort the stored input.

Using STL int main() { int input; vector<int> ivec; /* rest of code */ }

STL - Input while ( cin >> input ) ivec.push_back(input);

STL - Sorting sort(ivec.begin(), ivec.end()); Sort Prototype: void sort(Iterator first, Iterator last);

STL - Output Or (more recommended) for ( int i = 0; i < ivec.size(); ++i ) cout << ivec[i] << " "; cout << endl; Or (more recommended) vector<int>::iterator it; for ( it = ivec.begin(); it != ivec.end(); ++it ) cout << *it << " "; cout << endl;

STL - Include files #include <iostream> // I/O #include <vector> // container #include <algorithm> // sorting //using namespace std;

Putting it all together int main() { int input; vector<int> ivec; // input while (cin >> input ) ivec.push_back(input); // sorting sort(ivec.begin(), ivec.end()); // output vector<int>::iterator it; for ( it = ivec.begin(); it != ivec.end(); ++it ) { cout << *it << " "; } cout << endl; return 0; }

Operations on vector iterator begin(); iterator end(); bool empty(); void push_back(const T& x); iterator erase(iterator it); iterator erase(iterator first, iterator last); void clear(); ….