+ All Categories
Home > Documents > Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-1 Today’s Learning Objective ...

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

Date post: 08-Jan-2018
Category:
Upload: gwenda-grant
View: 215 times
Download: 1 times
Share this document with a friend
Description:
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
19
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-1 Today’s Learning Objective Standard Template Library
Transcript
Page 1: 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-1

Today’s Learning Objective Standard Template Library

Page 2: 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

Page 3: 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-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

Page 4: 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-4

Different Containers Sequential

1st element, next element, … to last element

Examples: vector, linked list

Associative Example: set and map

Page 5: 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-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

Page 6: 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-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

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

}

Page 8: 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-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

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

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;

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

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

Page 11: 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-11

Random Access of vector iterators

Page 12: 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-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

Page 13: 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-13

Display 19.4 Two Kinds of Lists

STL list is a double link list

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

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;

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

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

Page 16: 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-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

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

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

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

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

Page 19: 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-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


Recommended