Lecture 4

16

Click here to load reader

Transcript of Lecture 4

Page 1: Lecture 4

Vectors

Ref.: D.S. Malik, Data Structures Using C++

Page 2: Lecture 4

Vectors

• A vector is an array whose size can change.• Need to have:

#include <vector>

Page 3: Lecture 4

Declaring a vector

• vector <elementType> vecList;e.g. vector <int> intList;creates vector intList whose components are of

type int; it is emptye.g. vector <string> stringList;creates a vector whose components are of type

string

Page 4: Lecture 4

Declaring a vector

• vector <elementType> vecList(otherVecList);initialises vecList to the components of an

existing vector• vector <elementType> vecList(size);• vector <elementType> vecList(size,elem);initialises all components to elem

Page 5: Lecture 4

Declaring a vector

• vector <elementType> vecList(begin,end);initialises 1st component to begininitialises 2nd component to begin + 1…initialises last component to end - 1

Page 6: Lecture 4

Declaring a vector

• Student exercise:• Declare a vector intList whose components are

of type int and whose size is 10.

Page 7: Lecture 4

To copy the elements of an array to a vector

int intArray [5] = {2,4,6,8,10};vector <int> intList (intArray, intArray+5);

Page 8: Lecture 4

Accessing the elements of a vector

• vecList.at (index)• vecList [index]Both of the above return the element at position

indexe.g. vector <int> intList(5);

for (int j = 0; j < 5; j++)intList [j] = j;

Page 9: Lecture 4

Accessing the elements of a vector

• vecList.front() 1st element• vecList.back() last element

Page 10: Lecture 4

VectorsDescription

vecList.clear () deletes all elements

vecList.erase (position) deletes element located at position

vecList.erase (beg, end) deletes from beg to end-1

vecList.insert (position, elem) inserts elem in a new element located at position

vecList.insert (position, n, elem) inserts n elements

vecList.insert (position, beg, end) inserts a copy of the elements at locations beg to end-1

vecList.push_back (elem) inserts elem at end

vecList.pop_back () deletes last element

vecList.resize (num) changes vector size to num

vecList.resize (num, elem) change size and set newly created elements to elem

Page 11: Lecture 4

Vectors

• e.g.vector <int> intList;intList.push_back (34);intList.push_back (55);

• Student exercise: What does the above code do?

Page 12: Lecture 4

Pointer (aka iterator)

• declaration: vector<int>::iterator intVecIter;

declares an iterator called intVecIter• ++intVecItercauses the iterator to point to the next element

in a vector• *intVecIterreturns the value of the current vector element

Page 13: Lecture 4

vector <int> intList;vector <int>::iterator intVecIter;…intVecIter = intList.begin();…for (intVecIter = intList.begin(); intVecIter != intList.end(); ++intVecList)

cout << *intVecIter << “ “;

1. What’s happening?

2. What’s happening?

Page 14: Lecture 4

Answers

1. iterator points to 1st element2. outputs all elements in the vector

Page 15: Lecture 4

What does the vector look like after executing this?

int intArray [7] = {1,3,5,7,9,11,13};vector <int> vecList (intArray, intArray + 7);vector <int>::iterator intVecIter;intVecIter = vecList.begin();++intVecIter;vecList.insert (intVecIter, 22);

Page 16: Lecture 4

Answer

• {1, 22, 3, 5, 7, 9, 11, 13}