Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today’s Learning Objective

Similar presentations


Presentation on theme: "Today’s Learning Objective"— Presentation transcript:

1 Today’s Learning Objective
Standard Template Library Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

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.

3 Containers Container classes in STL Each has own iterator
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.

4 Different Containers Sequential Associative
1st element, next element, … to last element Examples: vector, linked list Associative Example: set and map Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

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.

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 Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

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.

8 Vector Container: Iterator: Difference between vector and array
vector<int> container; Iterator: vector<int>::iterator p; Difference between vector and array Array size is fixed, while vector size is flexible Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

9 Example: Iterators Used with a Vector (1 of 2)
#include <iostream> #include <vector> using namespace std; 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;

10 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:

11 Random Access of vector iterators
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

12 List Container: Iterator: Difference between vector and list
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 Display 19.4 Two Kinds of Lists
STL list is a double link list Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

14 Using the list Container(1 of 2)
#include <iostream> #include <list> using namespace std; 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(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"; 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 Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

17 Set Container example (1)
#include <iostream> #include <set> using namespace std; int main() { set<char> s; set<char>::iterator p; s.insert('A'); s.insert('D'); s.insert('C'); s.insert('B'); Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

18 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"; { } return 1; The set contains: A B C D Removing C: The Set contains A B D Copyright © 2006 Pearson Addison-Wesley. All rights reserved.

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 Copyright © 2006 Pearson Addison-Wesley. All rights reserved.


Download ppt "Today’s Learning Objective"

Similar presentations


Ads by Google