Presentation is loading. Please wait.

Presentation is loading. Please wait.

Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.

Similar presentations


Presentation on theme: "Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors."— Presentation transcript:

1 Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors

2 Glenn Stevenson CSIS 113A MSJC Vectors  Vector Introduction  Recall: arrays are fixed size  Vectors: ‘arrays that grow and shrink’  During program execution  Formed from Standard Template Library (STL)  Using template class

3 Glenn Stevenson CSIS 113A MSJC Vector Basics  Similar to array:  Has base type  Stores collection of base type values  Declared differently:  Syntax: vector  Indicates template class  Any type can be ‘plugged in’ to Base_Type  Produces ‘new’ class for vectors with that type  Example declaration: vector v;

4 Glenn Stevenson CSIS 113A MSJC Vector Syntax

5 Glenn Stevenson CSIS 113A MSJC Vector Use  vector v;  ’v is vector of type int’  Calls class default constructor  Empty vector object created  Indexed like arrays for access  But to add elements:  Must call member function push_back  Member function size()  Returns current number of elements

6 Glenn Stevenson CSIS 113A MSJC Using A Vector vector v; v.push_back(32 ); cout << "The first element in the vector is " << v[0] << endl;

7 Glenn Stevenson CSIS 113A MSJC Bracket Limitations You use the bracket when you want to retrieve an element from the vector or when you want to modify an existing element. What you cannot do is add a new element to the end of the vector using the brackets. –You must use the push_back function for this: vector v; v.push_back(32); // add data to first element v[0] v[1] = 21; // Can't do this, you must use push_back function to add new data

8 Glenn Stevenson CSIS 113A MSJC Vector Efficiency  Member function capacity()  Returns memory currently allocated  Not same as size()  Capacity typically > size  Automatically increased as needed  If efficiency critical:  Can set behaviors manually  v.reserve(32); //sets capacity to 32  v.reserve(v.size()+10); //sets capacity to 10 more than size

9 Glenn Stevenson CSIS 113A MSJC Initializing With The Constructor Vectors have a constructor that will allow you to set the initial capacity. For instance vector v(10); –Sets the initial capacity of the vector to 10 (0 - 9 ). –It also sets their values to 0. –You can then use the brackets to modify the values: –v[2] = 32;

10 Glenn Stevenson CSIS 113A MSJC Introduction To Iterators Vector is a look at first container class Iterator allows you to iterate through the collection in the container All container classes have them –We will see more later on Iterator is like a pointer that can be moved and examined

11 Glenn Stevenson CSIS 113A MSJC Declaring Iterators Part of the vector class –vector ::iterator name; Name can be anything that you want –It is an iterator variable that can be used with a vector

12 Glenn Stevenson CSIS 113A MSJC Setting the iterator Can be set to the beginning or end of vector. –Use begin() and end() methods vector v; vector ::iterator pEnd; pEnd = v.begin();

13 Glenn Stevenson CSIS 113A MSJC Looping through a vector Works A lot like a pointer! –Fill Array revisited vector v; vector ::iterator p; for(p = v.begin(); p < v.end(); p++) cout << *p << endl;

14 Glenn Stevenson CSIS 113A MSJC Erasing items in a vector Use the erase method –It takes an iterator as an argument vector v; vector ::iterator p; v.push_back(1);//Add elements to end of vector v.push_back(3); p = v.end():// Set iterator to last element v.erase(p);// erase last element Stack and queue!


Download ppt "Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors."

Similar presentations


Ads by Google