Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objective Standard Template Library
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Different Containers Sequential 1 st element, next element, … to last element Examples: vector, linked list Associative Example: set and map
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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 1 st item in c c.end();//Returns "test" value for end
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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 }
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Vector Container: vector container; Iterator: vector ::iterator p; Difference between vector and array Array size is fixed, while vector size is flexible
Example: Iterators Used with a Vector (1 of 2) #include using std::cout; using std::endl; using std::vector; int main( ) { vector container; for (int i = 1; i <= 4; i++) container.push_back(i); cout << "Here is what is in the container:\n"; vector ::iterator p; for (p = container.begin( ); p != container.end( ); p++) cout << *p << " "; cout << endl;
Example: Iterators Used with a Vector (2 of 2) cout << "Setting entries to 0:\n"; for (p = container.begin( ); p != container.end( ); p++) *p = 0; cout << "Container now contains:\n"; for (p = container.begin( ); p !=container.end( ); p++) cout << *p << " "; cout << endl; return 0; } Here is what is in the container: Setting entries to 0: Container now contains: 0 0
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Random Access of vector iterators
Copyright © 2006 Pearson Addison-Wesley. All rights reserved List Container: list container; Iterator: list ::iterator p; Difference between vector and list Vector allows random access while list do not allow
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display 19.4 Two Kinds of Lists STL list is a double link list
Using the list Container(1 of 2) #include using std::cout; using std::endl; using std::list; int main( ) { list LO; for (int i = 1; i <= 3; i++) { LO.push_back(i); LO.push_front(i); } cout << "List contains:\n"; list ::iterator iter; for (iter = LO.begin(); iter != LO.end(); iter++) cout << *iter << " "; cout << endl;
Using the list Container(2 of 2) 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: Setting all entries to 0: List now contains: 0 0 0
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Set Container Stores elements without repetition 1 st insertion places element in set Each element value is its own key Capabilities: Add elements Delete elements Ask if element is in set
Set Container example (1) #include using std::set; using std::cout; using std::endl; using std::set ::iterator; int main() { set s; iterator p; s.insert('A'); s.insert('D'); s.insert('C'); s.insert('B'); Copyright © 2006 Pearson Addison-Wesley. All rights reserved
Set Container example(2) cout << “The set contains:\n"; for (p=s.begin(); p!=s.end();p++) cout << *p << "\t"; cout << endl; s.erase('C'); cout << “The Set contains:\n"; for (p=s.begin(); p!=s.end();p++) { cout << *p << "\t"; } cout << endl; return 1; } Copyright © 2006 Pearson Addison-Wesley. All rights reserved The set contains: A B C D Removing C: The Set contains A B D
Copyright © 2006 Pearson Addison-Wesley. All rights reserved map Template Class Stores pairs of data Examples: The first data (key type) is its key Capabilities: Add elements Delete elements Ask if element is in map