main index contents 11 main index contents container types container types sequence containers...

38
1 Main Index Conten ts 1 Main Index Conten ts Container Types Sequence Containers Associative Containers Adapter Classes The List Container Stack Containers Queue Containers Priority Queue Containe rs Set Containers Map Containers C++ Arrays - Evaluating an Array as a Container Chapter 4 Chapter 4 The Vector Container The Vector Container Vectors - Vector Class Constru ctor –API (2 slides) - Vector Class Operati ons –API (4 slides) -Output with Vectors - Declaring Vector Obj ects - Adding and Removing Vector Elements -Resizing a Vector The Insertion Sort - Insertion Sort Algor ithm (3 slides) Template Class (2

Post on 20-Dec-2015

252 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

1 Main IndexMain Index ContentsContents1 Main IndexMain Index ContentsContents

Container TypesSequence ContainersAssociative ContainersAdapter ClassesThe List ContainerStack ContainersQueue ContainersPriority Queue ContainersSet ContainersMap Containers

C++ Arrays-Evaluating an Array as a Container

Chapter 4 Chapter 4 – – The Vector ContainerThe Vector Container

Vectors

-Vector Class Constructor –API (2 slides)-Vector Class Operations –API (4 slides)-Output with Vectors-Declaring Vector Objects-Adding and Removing Vector Elements-Resizing a Vector

The Insertion Sort

-Insertion Sort Algorithm (3 slides)

Template Class (2 slides)

Store Class (2 slides)

Summary Slides (6 slides)

Page 2: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

2 Main IndexMain Index ContentsContents2 Main IndexMain Index ContentsContents

Container TypesContainer Types

Sequence Containers

Adapter Containers

Associative Containers

Vector Stack Set, Multiset

Deque Queue Map, Mutltimap

List Priority Queue

Page 3: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

3 Main IndexMain Index ContentsContents3 Main IndexMain Index ContentsContents

Sequence ContainersSequence Containers

A sequence container stores data by position in linear order 1st element, 2nd element, and so forth.

P o s itio n 0 P o s itio n 4P o s itio n 3P o s itio n 2P o s itio n 1

S eq uenc e C o ntainer

Page 4: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

4 Main IndexMain Index ContentsContents4 Main IndexMain Index ContentsContents

Associative ContainersAssociative Containers Associative containers store elements by key.

– Ex: name, social security number, or part number.

A program accesses an element in an associative container by its key, which may bear no relationship to the location of the element in the container.

Page 5: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

5 Main IndexMain Index ContentsContents

Adapter ClassesAdapter Classes An adapter contains a sequence container as

its underlying storage structure. The programmer interface for an adapter

provides only a restricted set of operations from the underlying storage structure.

Page 6: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

6 Main IndexMain Index ContentsContents6 Main IndexMain Index ContentsContents

The List ContainerThe List Container

fro n t rear

n ext n extn extn ext

15f ro nt

46123

B e fo re

f ro nt

46123

Af te r

Inserting into a List Container

Page 7: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

7 Main IndexMain Index ContentsContents7 Main IndexMain Index ContentsContents

Stack ContainersStack Containers

A stack allows access at only one end of the sequence, called the top.

C

A to p

P u s h A

BA

to p

P u s h B

Bto p

P u s h C(a )

C

BA

to p

P o p C(b )

B

A to p

P o p B

A

Page 8: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

8 Main IndexMain Index ContentsContents8 Main IndexMain Index ContentsContents

Queue ContainersQueue Containers

A queue is a container that allows access only at the front and rear of the sequence.

A B C D

E

B C D E

A

rear fro ntIns ert D elete

Page 9: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

9 Main IndexMain Index ContentsContents9 Main IndexMain Index ContentsContents

Priority Queue ContainersPriority Queue Containers

A priority queue is a storage structure that has restricted access operations similar to a stack or queue.

Elements can enter the priority queue in any order. Once in the container, a delete operation removes the largest

(or smallest) value.

18

3

13

15

V alue = 8

27

Page 10: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

10 Main IndexMain Index ContentsContents10 Main IndexMain Index ContentsContents

Set ContainersSet Containers

A set is a collection of unique values, called keys or set members.

5

3

1

1527

S et A

F o rd

B uic k

H o nd a

S et B

B M W

Jeep

Jaguar

Page 11: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

11 Main IndexMain Index ContentsContents11 Main IndexMain Index ContentsContents

Map ContainersMap Containers

A map is a storage structure that implements a key-value relationship.

D 7 B -9 1 6

W 9 1 -A 8 3

4 .9 5

1 2 .5 0

M irage

C allo w ay

A 2 9 -4 6 8

D 7 B -9 1 6

W 9 1 -A 8 3

In d ex Ven d o rP riceP art #

A 2 9 -4 6 8 8 .7 5 M art in

Page 12: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

12 Main IndexMain Index ContentsContents12 Main IndexMain Index ContentsContents

C++ ArraysC++ Arrays

An array is a fixed-size collection of values of the same data type.

An array is a container that stores the n (size) elements in a contiguous block of memory.

arr[0 ] arr[1 ] arr[2 ]

0 1 2 n -1

. . . arr[n -1 ]

Page 13: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

13 Main IndexMain Index ContentsContents13 Main IndexMain Index ContentsContents

Evaluating an Array as a Evaluating an Array as a ContainerContainer

The size of an array is fixed at the time of its declaration and cannot be changed during the runtime.– An array cannot report its size. A separate integer

variable is required in order to keep track of its size.

C++ arrays do not allow the assignment of one array to another.– The copying of an array requires the generation of a

loop structure with the array size as an upper bound.

Page 14: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

14 Main IndexMain Index ContentsContents14 Main IndexMain Index ContentsContents

VectorsVectors

v [ 0 ] v [ 1 ] v [ 2 ] . . . v [ n-1 ] ro o m to gro w

0 1 2 n-1

Page 15: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

15 Main IndexMain Index ContentsContents15 Main IndexMain Index ContentsContents

CLASS vector Constructors <vector>

vector();Create an empty vector. This is the default

constructor. 

vector(int n, const T& value = T());Create a vector with n elements, each having a

specified value. If the value argument is omitted, the elements are filled with the default value for type T. Type T must have a default constructor, and the default value of type T is specified by the notation T().

Page 16: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

16 Main IndexMain Index ContentsContents16 Main IndexMain Index ContentsContents

CLASS vector Constructors <vector>

vector(T *first, T *last);Initialize the vector using the address range [first,

last). The notation *first and *last is an example of pointer notation that we cover in Chapter 5.

Page 17: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

17 Main IndexMain Index ContentsContents17 Main IndexMain Index ContentsContents

CLASS vector Operations <vector>

T& back();Return the value of the item at the rear of the vector.

Precondition: The vector must contain at least one element.

 const T& back() const;

Constant version of back().  

bool empty() const;Return true if the vector is empty and false

otherwise.

Page 18: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

18 Main IndexMain Index ContentsContents18 Main IndexMain Index ContentsContents

CLASS vector Operations <vector>

T& operator[] (int i);Allow the vector element at index i to be retrieved or

modified.Precondition: The index, i, must be in the range 0

i < n, where n is the number of elements in the vector.

Postcondition: If the operator appears on the left side of an assignment statement, the expression on the right side

modifies the element referenced by the index.  

const T& operator[] (int i) const;Constant version of the index operator.

Page 19: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

19 Main IndexMain Index ContentsContents19 Main IndexMain Index ContentsContents

CLASS vector Operations <vector>

void push_back(const T& value);Add a value at the rear of the vector.

Postcondition: The vector has a new element at the rear and its size increases by 1.

 void pop_back();

Remove the item at the rear of the vector.Precondition: The vector is not empty.Postcondition: The vector has a new element at

the rear or is empty.

Page 20: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

20 Main IndexMain Index ContentsContents20 Main IndexMain Index ContentsContents

CLASS vector Operations <vector>

void resize((int n, const T& fill = T());Modify the size of the vector. If the size is increased,

the value fill is added to the elements on the tail of the vector. If the size is decreased, the original

values at the front are retained.Postcondition: The vector has size n.

 int size() const;

Return the number of elements in the vector.

Page 21: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

21 Main IndexMain Index ContentsContents

Output with VectorsOutput with Vectors// number of elements in list is v.size()

template <typename T>

void writeVector(const vector<T>& v)

{// capture the size of the vector in n

int i, n = v.size();

 

for(i = 0; i < n; i++)

cout << v[i] << " ";

cout << endl;

}

Page 22: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

22 Main IndexMain Index ContentsContents

Declaring Vector ObjectsDeclaring Vector Objects// vector of size 5 containing the integer // value 0

vector<int> intVector(5);

9

43210

2 7 3 1 2

// vector of size 10; each element // contains the empty string

vector<string> strVector(10);

Page 23: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

23 Main IndexMain Index ContentsContents

Adding and Removing Adding and Removing Vector ElementsVector Elements

1 2 - 5 8 1 41 2 - 5 8 1 4

0

Bef o r e

v . s ize ( ) = 4

43210321

v . s ize ( ) = 5

Af ter v .p u s h _ b ac k ( 1 0 )

1 0

4 .64 .6 6 .8

0

B efo re

v .s iz e() = 2

01

v .s iz e() = 1

A ft er v .p o p _ b ack ()

Page 24: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

24 Main IndexMain Index ContentsContents

Resizing a VectorResizing a Vectorint arr[5] = {7, 4, 9, 3, 1};vector<int> v(arr,arr+5);

// v initially has 5 integers

v.resize(10);// list size is doubledv.resize(4); // list is contracted. data

// is lost

7 4 9

7 4 9 3 1 0 0 0 0

7 4 9 3

vec to r< int> v(arr,5)

v.res ize(10);

v.res ize(4);

1

0

3

Page 25: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

26 Main IndexMain Index ContentsContents26 Main IndexMain Index ContentsContents

The Insertion SortThe Insertion Sort

Ins e r t D ar e atl o c at i o n 1 ;the tai l o f the l i s ts h i ft s to the r i g ht

Star t w i th M o nr o e

C h inIns e r t C hi n i n l o c at i o n 0 ;

M o nr o e m o ve s to l o c at i o n 1

F lo resC h in Ins e r t F l o r e s i n l o c at i o n 1 ;M o nr o e m o ve s to l o c at i o n 2

C h in M o n ro e St einF lo res E l e m e nt Ste i n i s O K

C h in D are M o n ro e St einF lo res

P r o c e s s i ng F l o r e s

P r o c e s s i ng C hi n

P r o c e s s i ng D ar e

P r o c e s s i ng Ste i n

M o n ro e

M o n ro e

M o n ro e

Page 26: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

27 Main IndexMain Index ContentsContents27 Main IndexMain Index ContentsContents

Insertion Sort AlgorithmInsertion Sort AlgorithminsertionSort():// sort a vector of type T using insertion // sort

template <typename T>

void insertionSort(vector<T>& v)

{

int i, j, n = v.size();

T temp;

 // place v[i] into the sublist v[0] ... //

v[i-1], 1 <= i < n, so it is in the // correct position

Page 27: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

28 Main IndexMain Index ContentsContents28 Main IndexMain Index ContentsContents

Insertion Sort AlgorithmInsertion Sort Algorithmfor (i = 1; i < n; i++)

{ // index j scans down list from v[i] // looking for correct position to // locate target. assigns it to

v[j]

j = i;

temp = v[i]; // locate insertion point by scanning

// downward as long as temp < v[j-1] // and we have not encountered the // beginning of the list

Page 28: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

29 Main IndexMain Index ContentsContents29 Main IndexMain Index ContentsContents

Insertion Sort AlgorithmInsertion Sort Algorithmwhile (j > 0 && temp < v[j-1])

{ // shift elements up list to make // room for insertion

v[j] = v[j-1];

j--;

} // the location is found; insert temp

v[j] = temp;

}

}

Page 29: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

30 Main IndexMain Index ContentsContents30 Main IndexMain Index ContentsContents

Template ClassTemplate Class<template typename T>

class templateClass

{

public:

// constructor with argument of // constant reference type T

templateClass (const T& item);

// member function returns a value of // type T

T f();

Page 30: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

31 Main IndexMain Index ContentsContents31 Main IndexMain Index ContentsContents

Template ClassTemplate Class// member function has an argument of // type T

void g(const T& item);

...

private:

T dataValue;

...

};

Page 31: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

32 Main IndexMain Index ContentsContents32 Main IndexMain Index ContentsContents

CLASS store Declaration “d_store.h”

template <typename T>class store{public:store(const T& item = T()); // initialize value with item or the default // object of type T

 getValue() const; // retrieve and return data member value

Page 32: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

33 Main IndexMain Index ContentsContents33 Main IndexMain Index ContentsContents

CLASS store Declaration “d_store.h”

void setValue(const T& item); // update the data member value to item

friend ostream& operator<< (ostream& ostr, const store<T>& obj);

// display output in the from "Value = " // value

private:T value; // data stored by the object

};

Page 33: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

34 Main IndexMain Index ContentsContents34 Main IndexMain Index ContentsContents

Summary Slide 1Summary Slide 1

§- The Standard Template Library (STL) provides 10 container classes for solving a wide range of

problems.

§- Containers fall into one of three classifications:

1) sequence containers

2)2) adaptersadapters

3) associative containers.

Page 34: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

35 Main IndexMain Index ContentsContents35 Main IndexMain Index ContentsContents

Summary Slide 2Summary Slide 2

§- The array data structure is a sequence container.

- It defines a block of consecutive data values of the same type.- Arrays are direct access containers.

§- An index may be used to select any item in the list without referencing any of the other items

Page 35: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

36 Main IndexMain Index ContentsContents36 Main IndexMain Index ContentsContents

Summary Slide 3Summary Slide 3

§- The vector sequence container provides direct access through an index and grows dynamically at the rear as needed.

- Insertion and deletion at the rear of the sequence is very efficient

§- these operations inside a vector are not efficient.

Page 36: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

37 Main IndexMain Index ContentsContents37 Main IndexMain Index ContentsContents

Summary Slide 4Summary Slide 4

§- The list sequence container stores elements by position.

- List containers do not permit direct access§- must start at the first position (front) and move

from element to element until you locate the data value.

- The power of a list container is its ability to efficiently add and remove items at any position in the sequence.

Page 37: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

38 Main IndexMain Index ContentsContents38 Main IndexMain Index ContentsContents

Summary Slide 5Summary Slide 5

§- stacks and a queues are adapteradapter containers that restrict how elements enter and leave a

sequence.

- A stack allows access at only one end of the sequence, called the top.- A queue is a container that allows access only at

the front and rear of the sequence.§- Items enter at the rear and exit from the front.

§- Similar to a stack or queue, the priority queue adapter container restricts access operations.

§- Elements can enter the priority queue in any order.§- Once in the container, only the largest element

may be accessed.

Page 38: Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers

39 Main IndexMain Index ContentsContents39 Main IndexMain Index ContentsContents

Summary Slide 6Summary Slide 6

§- A set is a collection of unique values, called keys or set members.

- The set container has a series of operations that allow a programmer to determine if an item is a

member of the set and to very efficiently insert and delete items.

§- A map is a storage structure that allows a programmer to use a key as an index to the data.

- Maps do not store data by position and instead use key-access to data allowing a programmer to treat them as though they were a vector or array.