Download presentation
Presentation is loading. Please wait.
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 different container classes Each container class has "own" iterator type Similar to how each data type has own pointer type
6
Manipulating Iterators
overloaded operators: ++, --, ==, != * So if p is iterator variable, *p gives access to data pointed to by p Vector container has members begin() and end() c.begin(); //Returns iterator for 1st item in c c.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 (int i = 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: Setting entries to 0: Container now contains:
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"; cout << *iter << " "; cout << endl; return 0; } List contains: Setting all entries to 0: List now contains:
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: 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: 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 : 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:
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.