220 likes | 331 Views
Class 18. Become familiar with the STL functionalities To understand how algorithms use iterators to access the elements of STL continaers Use a templatized STL container. Objectives. Standard Template Library (STL). Three basic components containers a collection of objects algorithms
E N D
Become familiar with the STL functionalities To understand how algorithms use iterators to access the elements of STL continaers Use a templatized STL container Objectives
Standard Template Library (STL) • Three basic components • containers • a collection of objects • algorithms • a function for processing a container such as sort,search • iterators • mechanism for accessing the objects in a container
grow and shrink in size automatically contribute to programming ease use storage more efficiently can be subclassed to provide additional functionality allow the programmer to avoid the use of pointer/new/delete promote reusability and extensibility Can contain built-in or user-defined data types Advantages --STL containers
Learning what is available and how to use it Implemented differently on different machines The syntax can be messy and ambiguous Care needs to be taken so that the container classes work on arbitrary subclasses Disadvantages -- STL
seven basic ones divided into two groups sequential containers (elements can be access by position) list vector deque Container-- a data structure
associative containers (elements can be access by key) map multimap set multiset Container-- a data structure
sort search replace reverse copy swap access an element insert an element erase an element Algorithms on containers
Like an array but grows and shrinks atuomatically to meet its own storage needs Assumes that the parametized type T has default and copy constructors as well as equality (operator ==) and a less-than (operator < ) overloaded #include <vector> Vectors
Pg. 942 Fig. 20.14 # include <iostream> # include <vector> using namespace std; template <class T> void printVector (const vector <T> &vec) int main() { const int SIZE = 6; vector < int > v; cout << v.size() << v.capacity();
Pg. 942 Fig. 20.14 v.push_back(2); v.push_back(3); v.push_back(4); cout << v.size() << v.capacity(); printVector(v);
template < class T> void printVector (const vector <T> &vec) { vector < T > :: const_iterator p1; for (p1= vec.begin(); p1 != vec.end() ; ++p1) cout << * p1; }
maincontinued ….. vector < int > :: reverse_iterator p2; for ( p2 = v.rbegin(); p2 ! = v.rend() ; ++ p2) cout << *p2;
Pg. 945 Fig. 20.15 # include <iostream> # include <vector> # include <algorithm> using namespace std; int main() { const int SIZE = 6; int a[SIZE] = {1, 2, 3, 4, 5, 6}; vector < int > v ( a, a + SIZE); ostream_iterator <int> output( cout, “ “); copy (v.begin(), v.end(), output);
cout << v.front(); cout << v.back(); v[0] = 7; v.at(2) = 10; v.insert(v.begin() + 1,22); copy (v.begin(),v.end(),output); v.erase( v.begin()); v.erase(v.begin(),v.end());
Analysis of sequential container operations Operation Vector Deque List Insert/erase at Linear Constant Constant beginning Insert/erase at Constant Constant Constant end Insert/erase in Linear Linear Constant middle
Analysis of sequential container operations Operation Vector Deque List Access first element Constant Constant Constant Access last element Constant Constant Constant Access middle Constant Constant Linear element
vector < int > v (10); // fill up vector generate (v.begin(), v.end(), rand); // other algorithms int sq ( int x) { return x * x; } void dump ( int i) { cout << i << endl; } for.each (v.begin( ) , v.end( ), sq); for.each( v.begin(), v.end(), dump); sort( v.begin( ), v.end( ));
Algorithms on C++ arrays # include <iostream> # include <algorithm> using namespace std; int main () { char alph[ ] = “afgcstdeuvijpmohnxwrqs”; ostream_iterator < char > output (cout, “ “); nth.element(alph, alph + 12, alph + 22); random.shuffle(alph,alph + 12); random.shuffle(alph + 12, alph + 22); copy ( alph, alph + 12, output); }
Additional algorithms cout << max (‘q’, ‘z’); int a = 5 , b = 3; cout << min ( a, b);