1 / 20

Standard Template Library (STL)

Standard Template Library (STL). Standard Template Library. Recall array and linkedlist structures Standard Template Library (STL) Includes libraries for all such data structures Each data structure in STL contains Containers: Like data structures Iterators: Like pointers. Containers.

eponder
Download Presentation

Standard Template Library (STL)

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. Standard Template Library (STL)

  2. Standard Template Library • Recall array and linkedlist structures • Standard Template Library (STL) • Includes libraries for all such data structures • Each data structure in STL contains • Containers: Like data structures • Iterators: Like pointers

  3. Containers • Container classes in STL • Each is template class with parameter for particular data type to be stored • e.g., Lists of int • Each has own iterator

  4. Different Containers • Sequential • 1st element, next element, … to last element • Examples: vector, linked list • Associative • Example: set and map

  5. Iterators • Generalization of a pointer • Typically even implemented with pointer • Designed to hide details of implementation • Provide uniform interface across differentcontainer classes • Each container class has "own" iterator type • Similar to how each data type has ownpointer type

  6. Manipulating Iterators • overloaded operators: • ++, --, ==, != • * • So if p is iterator variable, *p gives access to datapointed to by p • Vector container • has members begin() and end() • c.begin(); //Returns iterator for 1st item in cc.end(); //Returns "test" value for end

  7. Cycling with Iterators • Cycling ability (c is a container; p is a iterator): for (p=c.begin(); p!=c.end(); p++) { //process *p //*p is current data item }

  8. Vector • Container: vector<int> c; • Iterator: vector<int>::iterator p; • Difference between vector and array • Array size is fixed, while vector size is flexible

  9. Iterators Used with a Vector #include <iostream> #include <vector> int main( ){ vector<int> c; vector<int>::iterator p; for (inti = 1; i <= 4; i++) // initialize vector c.push_back(i); cout << "Here is what is in the container:\n"; for (p = c.begin( ); p != c.end( ); p++) cout << *p << " "; cout << endl;

  10. Iterators Used with a Vector cout << "Setting entries to 0:\n"; for (p = c.begin( ); p != c.end( ); p++) *p = 0; cout << "Container now contains:\n"; for (p = c.begin( ); p !=c.end( ); p++) cout << *p << " "; cout << endl; return 0; } Here is what is in the container: 1 2 3 4 Setting entries to 0: Container now contains: 0 000

  11. Random Access of vector iterators

  12. List • Container: list<int> container; • Iterator: list<int>::iterator p; • Difference between vector and list: • Vector allows random access while list do not allow

  13. Two Kinds of Lists STL list is a double link list

  14. Using the list Container #include <iostream> #include <list> int main( ){ list<int> LO; for (int i = 1; i <= 3; i++) { LO.push_back(i); LO.push_front(i); } cout << "List contains:\n"; list<int>::iterator iter; for (iter = LO.begin(); iter != LO.end(); iter++) cout << *iter << " "; cout << endl;

  15. Using the list Container cout << "Setting all entries to 0:\n"; for (iter = LO.begin(); iter != LO.end(); iter++) *iter = 0; cout << "List now contains:\n"; for (iter = LO.begin(); iter != LO.end(); iter++) cout << *iter << " "; cout << endl; return 0; } List contains: 3 2 1 1 2 3 Setting all entries to 0: List now contains: 0 0 0 0 0 0

  16. Set Container • Stores elements without repetition • 1st insertion places element in set • Each element value is its own key • Capabilities: • Add elements • Delete elements • Ask if element is in set

  17. #include <set> int main() { int num; bool found = false; set<int> s; set<int>::iterator p; for (int i=1; i<=5; i++) s.insert(i*10); // init set: 10 20 30 40 50 cout << "Set contains: " ; for (p = s.begin(); p != s.end(); p++) cout << ' ' << *p; cout << '\n'; cout << "Enter a number to look for in the set: " ; cin >> num; p = s.find(num); if (p != s.end()) cout << "Found " << (*p) << " in set S\n"; else cout << "Not found\n"; }

  18. cout << "\nEnter a number to erase from set: " ; cin >> num; if (s.find(num)!= s.end()) { // don't erase if not in set s.erase(num); cout << "Erasing " << num << " from set .....\n"; } else cout << num << " is not in set and can't be erased\n"; cout << "Set contains: " ; for (p = s.begin(); p != s.end(); p++) cout << " " << *p; cout << "\n\n"; }

  19. Set contains: 10 20 30 40 50 Enter a number to look for in the set: 20 Found 20 in set S Enter a number to erase from set: 35 35 is not in set and can't be erased Set contains: 10 20 30 40 50 : : Set contains: 10 20 30 40 50 Enter a number to look for in the set: 25 Not found Enter a number to erase from set: 30 Erasing 30 from set ..... Set contains: 10 20 40 50

  20. map Template Class • Stores pairs of data • <key type, value type> • Examples: • <SSN, Student GPA> • <Course Number, Course Name> • The first data (key type) is its key • Capabilities: • Add elements • Delete elements • Ask if element is in map

More Related