Date post: | 08-Jan-2018 |
Category: |
Documents |
Upload: | gwenda-grant |
View: | 215 times |
Download: | 1 times |
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-1
Today’s Learning Objective Standard Template Library
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-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
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-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
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-4
Different Containers Sequential
1st element, next element, … to last element
Examples: vector, linked list
Associative Example: set and map
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-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
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-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
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-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
}
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-8
Vector
Container: vector<int> container;
Iterator: vector<int>::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 <iostream>#include <vector>using std::cout;using std::endl;using std::vector;
int main( ){
vector<int> container;
for (int i = 1; i <= 4; i++)container.push_back(i);
cout << "Here is what is in the container:\n";vector<int>::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:1 2 3 4Setting entries to 0:Container now contains:0 0 0 0
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-11
Random Access of vector iterators
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-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
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-13
Display 19.4 Two Kinds of Lists
STL list is a double link list
Using the list Container(1 of 2)#include <iostream>#include <list>using std::cout;using std::endl;using std::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;
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:3 2 1 1 2 3Setting all entries to 0:List now contains:0 0 0 0 0 0
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-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
Set Container example (1)#include <iostream>#include <set>using std::set;using std::cout;using std::endl;using std::set<char>::iterator;
int main(){
set<char> s;iterator p;
s.insert('A');s.insert('D');s.insert('D');s.insert('C');s.insert('C');s.insert('B');
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-17
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. 13-18
The set contains:A B C DRemoving C:The Set containsA B D
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-19
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