Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-1 Today’s Learning Objective  Standard Template Library."— Presentation transcript:

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

2 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

3 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

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

5 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

6 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-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 1 st item in c c.end();//Returns "test" value for end

7 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 }

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

9 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;

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: 1 2 3 4 Setting entries to 0: Container now contains: 0 0

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

12 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-12 List  Container:  list container;  Iterator:  list ::iterator p;  Difference between vector and list  Vector allows random access while list do not allow

13 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-13 Display 19.4 Two Kinds of Lists STL list is a double link list

14 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;

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"; 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

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

17 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. 13-17

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"; 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 D Removing C: The Set contains A B D

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


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

Similar presentations


Ads by Google