12
Vectors the better arrays

Vectors the better arrays. Why Vectors l vectors are implemented as a class (a template to be exact)…

Embed Size (px)

DESCRIPTION

Declaration include header file using std::vector; l to declare vector vectorName ; where typeParameter – type/class of vector elements –corresponds to base type of the array examples vector items; // declares vector with no elements vector items(5); // declares vector with 5 elements vector items(5); // declares a vector of 5 objects of myclass 3

Citation preview

Page 1: Vectors the better arrays. Why Vectors l vectors are implemented as a class (a template to be exact)…

Vectors

the better arrays

Page 2: Vectors the better arrays. Why Vectors l vectors are implemented as a class (a template to be exact)…

Why Vectors vectors are implemented as a class (a template to be exact) they serve the same purpose as arrays but using object oriented

mechanisms (such as operator overloading) made more powerful vectors can

can grow and shrink as needed can be compared, assigned as a whole can be passed by reference/value/returned vector size is always available still more …

part of so called Standard Template Library (STL) once you learned vectors, do not use (raw) arrays

2

Page 3: Vectors the better arrays. Why Vectors l vectors are implemented as a class (a template to be exact)…

Declaration

include <vector> header file using std::vector; to declare

vector<typeParameter> vectorName;where

typeParameter – type/class of vector elements – corresponds to base type of the array

examplesvector<int> items; // declares vector with no elementsvector<double> items(5); // declares vector with 5 elementsvector<myclass> items(5); // declares a vector of 5 objects of myclass

3

Page 4: Vectors the better arrays. Why Vectors l vectors are implemented as a class (a template to be exact)…

Typical Array Operations indexing: items[3]

sill cannot refer past vector size: items[6]++; // range error

checking size: items.size() careful with this one: returns value of type size_t

example:for(size_t i=0; i < items.size(); ++i)

cout << items[i];

4

Page 5: Vectors the better arrays. Why Vectors l vectors are implemented as a class (a template to be exact)…

Atypical Array Operations adding element: items.push_back(55); removing element: items.pop_back(); checking if empty: items.empty(); accessing first/last element: items.front(); items.back(); changing size: items.resize(newSize); emptying: items.erase(); example:

int num=0;cin >> num;while(num !=0){

items.push_back(num); cin >> num;

}while(!items.empty()){

cout << items.back(); items.pop_back();

} 5

Page 6: Vectors the better arrays. Why Vectors l vectors are implemented as a class (a template to be exact)…

With Functions, Other Aggregate Operations

can pass by value/reference, returnvoid myfunc(vector<int>);void myfunc(vector<int>&);vector<int> myfunc();

can assign vector<int> v;v = items;

can swap: items.swap(v);

can compare: if(v == items) …

6

Page 7: Vectors the better arrays. Why Vectors l vectors are implemented as a class (a template to be exact)…

With Objectsclass intData{ // elementary class definitionpublic: void set(int n){data=n;} // simple setter int get() const{return data;} // simple accessorprivate: int data;};… vector<intData> v(5); // declaring a vector of objectsfor(int i=0; i<5; ++i){ // using vector as array int num; cin >> num; v[i].set(num); // invoking a method on vector element}

7

Page 8: Vectors the better arrays. Why Vectors l vectors are implemented as a class (a template to be exact)…

Iterators

iterator is a generalization of pointer not a pointer but usually implemented using pointers further enhances the power of vector

declaring iteratorvector<typeParameter>::iterator iteratorName;where

typeParameter – type/class of elements example

vector<int>::iterator ip;

8

Page 9: Vectors the better arrays. Why Vectors l vectors are implemented as a class (a template to be exact)…

Iterators Operations operations

++ (pre- and postfix) and -- to advance to the next (previous) data item +5 -3 advance certain number of positions forward/backward = = and != operators to test whether two iterators point to the same data item < > to compare if two elements are before or after in the vector dereferencing (*) provides data item access

functions v.begin() returns an iterator pointing to the first element of container v v.end() returns an iterator pointing past the last element of container v useful to

compare for end of iteration inserting: v.insert(ip, 22); // inserts one element at

// iterator ip position what does this do? v.insert(v.end()-2, 55);

erasing: v.erase(ip); // erases one element at // iterator ip position

sorting elements: sort(v.begin(),v.end()); need algorithm header

caution: after using iterator for update, iterators are invalidated, have to reassign

9

Page 10: Vectors the better arrays. Why Vectors l vectors are implemented as a class (a template to be exact)…

Iterator Usage Examplevector <int> v; // declare vector…for ( // declare and initialize iterator vector<int>::iterator p = v.begin(); p != v.end(); // check if end is not reached ++p // move iterator to next element ){ cout << *p; // manipulate element in loop body}

10

Page 11: Vectors the better arrays. Why Vectors l vectors are implemented as a class (a template to be exact)…

Typedef variable declarations can get long and obscure program meaning typedef – allows to give names to already defined types and classes

note: no new types declared benefits: shortens the code, gives more intuitive names, adds portability – can

change defined type later if needed syntax:

typedef existingType newType;

exampletypedef vector<int> myVector;typedef myVector::iterator myVectorIterator;myVector v;for (myVectorIterator p=v.begin(); p != v.end(); ++p){ cout << *p;

do not overdo – typedefs hide original types: give new types intuitive names, do not use for short/understandable types

11

Page 12: Vectors the better arrays. Why Vectors l vectors are implemented as a class (a template to be exact)…

Questions on Vectors why use vectors instead of arrays? what header file is needed to use

vectors? how is vector declared? how to declare a vector of five integers? how can one determine the size of the vector? What is the size of

this vector vector<int> myvector(4); how does one add/remove elements from a vector? how does one get the first/last element from a vector? can vectors be assigned to each other? What happens to their size?

contents? compared with each other? when are they equal? can vectors be passed by value? reference? returned? can vectors contain objects? what is iterator? how is it used? what is typedef? How is it used? What are begin() and end()?

12