STL: C++ Standard Library Standard Template Library (STL) Encapsulates complex data structures and algorithms is a library of generic container classes which are both efficient and functional C++ STL developed in early 90's at Hewlett Packard Laboratories Alex Stepanov and Meng Lee became part of C++ standard in 1994 implementations available by late 90's CSC 330 OO Software Design 1 CSC 330 OO Software Design 2 Main Ideas STL components General purpose: generic data structures & algorithms, templates Flexibility: Allows for many combinations of algorithm-container Simple & uniform interface: interface through templates (not inheritence) Efficiency Containers templates for classes which hold a collection of elements Algorithms templates for functions which operate on a range of elements from a container range is specified by iterators Iterators give access to the elements in a container allow for movement from one element to another Container classes Iterators Algorithms CSC 330 OO Software Design 3 CSC 330 OO Software Design 4 Components Containers: data structures vector, list, map, set, deque Adaptors: high-level data structure stack, queue, priority_queue Iterators: allow access into containers Algorithms: base algorithms sort, copy, search, min, max, Streams: input/output String Containers CSC 330 OO Software Design 5 CSC 330 OO Software Design 6 1
Basic Sequential Containers vector<t> growable, self-contained, type-independent array element type specified when a vector is declared vector<double> numbers; vector<cashier> checkoutstations; has a set of operations (methods) capacity increases when needed some vectors vector<int> v1; (capacity is 0) vector<float> v2 (10); (capacity is 10; size is 10) vector<string> v3 (5, "C++"); (capacity is 5; size is 5) CSC 330 OO Software Design 7 CSC 330 OO Software Design 8 Some vector<t> methods V.capacity( ) //size of array currently allocated V.size( ) //number of values V contains V.empty( ) //true iff V.size( ) is 0 V.reserve(n) //grow V so its capacity is n V.push_back(val) //add val at end of V V.pop_back( ) //erase V's last element V[i] //access element of V whose index is i V.at(i) //access element of V whose index is i CSC 330 OO Software Design 9 #include <iostream> #include <vector> using namespace std; bool Search(const vector<int> & V, int item); int main ( ) { vector<int> numbers; int number; while (cin >> number) { if (Search(numbers, number)) cout << "Duplicate" << endl; else numbers.push_back(number); // enter <control> D to stop the loop cout << "number of unique values: " << numbers.size( ); return 0; bool Search(const vector<int> & V, int item) { int p = 0; while(p < V.size( ) ) if (item = = V[p]) // or V.at(p) return true; else p++; return false; CSC 330 OO Software Design 10 list<t> class a list<t> data structure another STL container class used for storing a linear collection of like items comparison to a vector? linked list vs array is the underlying data structure no indexing (iterators are bidirectional) inserts and deletes anywhere are done in a constant amount of time head size 2 ------ CSC 330 OO Software Design 11 CSC 330 OO Software Design 12 2
Basic list class methods Some more list methods list( ); // construct an empty list list (const list<t> & alist); // copy constructor ~list( ); // destructor list<t> operator= (const list<t> & alist); // assignment operator bool empty( ); int size( ); L.push_back(value) // append value to L L.push_front(value) // insert value at front of L L.insert(pos, value) // insert value into L at // position indicated by iterator pos L.front( ) // return L's first element L.back( ) // return L's last element L.begin( ) // return an iterator positioned at start L.end( ) // return the"past the end" iterator L.sort( ) // sort L's elements using < CSC 330 OO Software Design 13 Why not sort (L.begin( ), L.end( ));? CSC 330 OO Software Design 14 #include <list> 9 #include <iostream> 7 using namespace std; 5 int main ( ) { 3 list<int>l; 10 L.push_back (9); 8 L.push_back (7); 6 L.push_back (5); 4 L.push_back (3); Press any key to continue list<int>::iterator p; for (p = L.begin ( ); p!= L.end ( ); p++) cout << *p << endl; for (p = L.begin ( ); p!= L.end ( ); p++) (*p)++; for (p = L.begin ( ); p!= L.end ( ); p++) cout << *p << endl; return 0; CSC 330 OO Software Design 15 Associative Containers CSC 330 OO Software Design 16 Basic Associative Containers Instantiating STL Container CSC 330 OO Software Design 17 CSC 330 OO Software Design 18 3
Iterators Member Functions CSC 330 OO Software Design 19 CSC 330 OO Software Design 20 Member Functions of Sequential Container Examples CSC 330 OO Software Design 21 CSC 330 OO Software Design 22 #include <iostream> #include <vector> #include <algorithm> using namespace std; int main ( ) { vector<int> numbers; int number; while (cin >> number) { if (find (numbers.begin ( ), numbers.end ( ), number)!= numbers.end ( )) cout << "Duplicate" << endl; else numbers.push_back(number); cout << "number of unique values: " << numbers.size( ); return 0; CSC 330 OO Software Design 23 #include <iostream> #include <vector> #include <algorithm> #include <cstdlib> using namespace std; void set (int & val); void display (int val); int main( ) { vector<int> A(5); for_each (A.begin ( ), A.end ( ), set); // would not work if vector<int> A; used for_each (A.begin ( ), A.end ( ), display); cout << endl; sort (A.begin ( ), A.end ( )); // operator< must be defined for A's element type for_each (A.begin ( ), A.end ( ), display); cout << endl; return 0; void set (int & val) { val = rand ( ); void display (int val) { cout << " " << val; 41 18467 6334 26500 19169 41 6334 18467 19169 26500 Press any key to continue CSC 330 OO Software Design 24 4
STL Iterators STL iterators Iterators are allow to traverse sequences Methods operator* operator++, and operator Different types of iterators - to support read, write and random access Containers define their own iterator types Changing the container can invalidate the iterator iterators are "pointer-like" objects provide a generic way to access the elements of any container class many STL algorithms require iterators as arguments some STL algorithms return an iterator each STL container class has an iterator class associated with it vector<t>::iterator list<t>::iterator stack<t> and queue<t> don't have iterators why? CSC 330 OO Software Design 25 CSC 330 OO Software Design 26 Iterators Iterator categories category determines available operations forward iterator iter++ (increment) *iter (dereference) == and!= (equality comparison) bidirectional iterator adds iter-- (decrement) random-access iterator adds iter[n] (constant time access to arbitrary element) iter =+ n (increment n times) CSC 330 OO Software Design 27 CSC 330 OO Software Design 28 Iterator Types Output Input Forward Bi-directional Random Read x = *i x = *i x = *i x = *i Write *i = x *i = x *i = x *i = x Iteration ++ ++ ++ ++, -- ++, --, +, -, +=, -= Comparison ==,!= ==,!= ==,!= ==,!=, <, >, <=, >= Output: write only and can write only once Input: read many times each item Forward supports both read and write Bi-directional support also decrement Random supports random access (just like C pointer) Iterators & Containers Bidirectional iterators: list, map, set Random access iterators: vector, deque Input/output/forward iterators: iostreams CSC 330 OO Software Design 29 CSC 330 OO Software Design 30 5
Iterators and Containers Data Access T::iterator iterator type for type T begin() front of the container end() element after last Container C Container::iterator i for( i = C.begin(); i!= C.end(); i++) // do something with *i CSC 330 OO Software Design 31 CSC 330 OO Software Design 32 Data Insertion Constant Iterators CSC 330 OO Software Design 33 CSC 330 OO Software Design 34 Example STL Algorithms some examples of STL algorithms find (iter1, iter2, value) //returns an iterator max_element (iter1, iter2) //returns an iterator sort (iter1, iter2) //sorts using < for_each (iter1, iter2, F) //applies F to every //item see STL Programmer's Guide link on home page CSC 330 OO Software Design 35 CSC 330 OO Software Design 36 6
Algorithms STL Algorithms are function templates designed to operate on a sequence of elements rather than methods the sequence is designated by two iterators most container classes have the following two methods begin( ) - returns an iterator positioned at the container's first element end( ) - returns an iterator positioned past the container's last element (past-theend) C.begin( ), C.end( ) specifies a sequence which contains all elements of the container C CSC 330 OO Software Design 37 CSC 330 OO Software Design 38 Algorithms Most STL algorithms works on sequences Sequences are passed as two iterators: beginning element element one after last p q Algorithms cont d sequence [p,q) Algorithms depend on iterator type not on container type CSC 330 OO Software Design 39 CSC 330 OO Software Design 40 Algorithms cont d Using sort( ) CSC 330 OO Software Design 41 CSC 330 OO Software Design 42 7
Using sort( ) cont d Function Objects CSC 330 OO Software Design 43 CSC 330 OO Software Design 44 Iterators and Algorithms More Iterators CSC 330 OO Software Design 45 CSC 330 OO Software Design 46 Discussions Plugging Iterators CSC 330 OO Software Design 47 CSC 330 OO Software Design 48 8
Discussions for_each( ( ) algorithm CSC 330 OO Software Design 49 CSC 330 OO Software Design 50 Discussions Associative Containers set class CSC 330 OO Software Design 51 CSC 330 OO Software Design 52 map class Container Adaptor CSC 330 OO Software Design 53 CSC 330 OO Software Design 54 9