1 / 22

Class 18

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

jared
Download Presentation

Class 18

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Class 18

  2. 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

  3. 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

  4. 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

  5. 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

  6. seven basic ones divided into two groups sequential containers (elements can be access by position) list vector deque Container-- a data structure

  7. associative containers (elements can be access by key) map multimap set multiset Container-- a data structure

  8. sort search replace reverse copy swap access an element insert an element erase an element Algorithms on containers

  9. 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

  10. 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();

  11. Pg. 942 Fig. 20.14 v.push_back(2); v.push_back(3); v.push_back(4); cout << v.size() << v.capacity(); printVector(v);

  12. template < class T> void printVector (const vector <T> &vec) { vector < T > :: const_iterator p1; for (p1= vec.begin(); p1 != vec.end() ; ++p1) cout << * p1; }

  13. maincontinued ….. vector < int > :: reverse_iterator p2; for ( p2 = v.rbegin(); p2 ! = v.rend() ; ++ p2) cout << *p2;

  14. 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);

  15. 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());

  16. 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

  17. 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

  18. 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( ));

  19. 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); }

  20. Additional algorithms cout << max (‘q’, ‘z’); int a = 5 , b = 3; cout << min ( a, b);

  21. Q & A

More Related