main index

45
Main Index Conten ts 1 1 Main Index Conten ts Week 3 – The Vector Container

Upload: kaori

Post on 06-Jan-2016

30 views

Category:

Documents


1 download

DESCRIPTION

1. Main Index. Contents. Week 3 – The Vector Container. . . . 3. Main Index. Contents. C++ 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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Main Index

Main IndexMain Index ContentsContents11 Main IndexMain Index ContentsContents

Week 3– The Vector Container

Page 2: Main Index

Main IndexMain Index ContentsContents2

Page 3: Main Index

Main IndexMain Index ContentsContents33 Main IndexMain Index ContentsContents

C++ 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 ]

int arr[] = {1,2,3,4,5};cout << arr[4];

Page 4: Main Index

Main IndexMain Index ContentsContents44 Main IndexMain Index ContentsContents

Evaluating an Array as a Container

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 5: Main Index

Main IndexMain Index ContentsContents

Variable-sized array

Use dynamically allocated memory to determine the size of the array at run time

Programmers need to handle array resize manually STL makes this easier

5

int h, *b;cin >> h;b = new int[h];...delete [] b;

Page 6: Main Index

Main IndexMain Index ContentsContents6

1. Allow for dynamic resizing2. Have a way to store the size internally3. Allow for assignment of one object to another

6 Main IndexMain Index ContentsContents

Vectors

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

0 1 2 n-1

• A container is a class that stores a collection of data• It has operations that allow a programmer to insert, erase, and update elements in the collection

Page 7: Main Index

Main IndexMain Index ContentsContents77 Main IndexMain Index ContentsContents

CLASS vector Constructors <vector>

http://www.cplusplus.com/reference/vector/vector/

Page 8: Main Index

Main IndexMain Index ContentsContents8

Declaring Vector Objects// vector of size 5 containing the

integer // value 0

vector<int> intVector(5);

// After assigning value for each element:

9

43210

2 7 3 1 2

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

string

vector<string> strVector(10);

Page 9: Main Index

Main IndexMain Index ContentsContents99 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.

  

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

otherwise.

Page 10: Main Index

Main IndexMain Index ContentsContents1010 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.

Page 11: Main Index

Main IndexMain Index ContentsContents1111 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 12: Main Index

Main IndexMain Index ContentsContents12

Adding and Removing Vector 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 te r 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 13: Main Index

Main IndexMain Index ContentsContents1313 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 14: Main Index

Main IndexMain Index ContentsContents14

Resizing 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 15: Main Index

Main IndexMain Index ContentsContents15

Output 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 16: Main Index

Main IndexMain Index ContentsContents

C++ interview questions on “Vector”

16

What do vectors represent?a) Static arraysb) Dynamic arraysc) Stackd) Queue

Answer: bExplanation: Vectors are sequence containers representing arrays that can change in size.

More questions here

Page 17: Main Index

Main IndexMain Index ContentsContents 17

Page 18: Main Index

Main IndexMain Index ContentsContents 18

Page 19: Main Index

Main IndexMain Index ContentsContents 19

Page 20: Main Index

Main IndexMain Index ContentsContents 20

Page 21: Main Index

Main IndexMain Index ContentsContents 21

Page 22: Main Index

Main IndexMain Index ContentsContents 22

Page 23: Main Index

Main IndexMain Index ContentsContents 23

Page 24: Main Index

Main IndexMain Index ContentsContents 24

Page 25: Main Index

Main IndexMain Index ContentsContents 25

Page 26: Main Index

Main IndexMain Index ContentsContents 26

Page 27: Main Index

Main IndexMain Index ContentsContents 27

Page 28: Main Index

Main IndexMain Index ContentsContents

Insertion Sort Animation

Pseudo-code

28

Page 29: Main Index

Main IndexMain Index ContentsContents

Insertion Sort "swap" operation in-place as 

temp ← A[j];

A[j] ← A[j-1];

A[j-1] ← temp;

29

Page 30: Main Index

Main IndexMain Index ContentsContents3030 Main IndexMain Index ContentsContents

Insertion Sort ImplementationinsertionSort():// 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 31: Main Index

Main IndexMain Index ContentsContents3131 Main IndexMain Index ContentsContents

Insertion Sort Implementationfor (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 32: Main Index

Main IndexMain Index ContentsContents3232 Main IndexMain Index ContentsContents

Insertion Sort Implementationwhile (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 33: Main Index

Main IndexMain Index ContentsContents

Time Complexity (Big O)

33

Page 34: Main Index

Main IndexMain Index ContentsContents

Worst case Best case: input is an array that is already

sorted. Linear running time (i.e., Θ(n)).

Worst case: input is an array sorted in reverse order. Quadratic running time (i.e., O(n2)).

34

Page 35: Main Index

Main IndexMain Index ContentsContents35

Underlying storage structure

Container classes

The storage structure should be selected so thatthe container operations can be implementedefficiently

Page 36: Main Index

Main IndexMain Index ContentsContents3636 Main IndexMain Index ContentsContents

Container Types

Sequence Containers

Adapter Containers

Associative Containers

Vector Stack Set, Multiset

Deque Queue Map, Mutltimap

List Priority Queue

Page 37: Main Index

Main IndexMain Index ContentsContents

Details on STL Containers

37

http://www.cplusplus.com/reference/stl/

Page 38: Main Index

Main IndexMain Index ContentsContents3838 Main IndexMain Index ContentsContents

Sequence 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 39: Main Index

Main IndexMain Index ContentsContents3939 Main IndexMain Index ContentsContents

The 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 40: Main Index

Main IndexMain Index ContentsContents40

Adapter 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 41: Main Index

Main IndexMain Index ContentsContents4141 Main IndexMain Index ContentsContents

Stack Containers (LIFO)

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

Page 42: Main Index

Main IndexMain Index ContentsContents4242 Main IndexMain Index ContentsContents

Queue Containers (FIFO)

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

Page 43: Main Index

Main IndexMain Index ContentsContents4343 Main IndexMain Index ContentsContents

Associative 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 44: Main Index

Main IndexMain Index ContentsContents4444 Main IndexMain Index ContentsContents

Set 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 45: Main Index

Main IndexMain Index ContentsContents4545 Main IndexMain Index ContentsContents

Map 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