Vectors In General and in the Standard Template

  • Slides: 32
Download presentation
Vectors In General and in the Standard Template Library Creative Commons License – Curt

Vectors In General and in the Standard Template Library Creative Commons License – Curt Hill.

Introduction • The book does not say much on vectors • Somewhat justified, since

Introduction • The book does not say much on vectors • Somewhat justified, since they are so much like an array • They make a gentle introduction to the STL Container Classes • First, about vectors in general Creative Commons License – Curt Hill.

Similar to an array • A sequence of elements • Should have contiguous storage

Similar to an array • A sequence of elements • Should have contiguous storage – One solid chunk of memory • Allows random access – Constant time subscripting – In languages, like C++, the subscripting operator should be overloaded – In others there is usually an at or index function Creative Commons License – Curt Hill.

Different from an array • The size may be changed – Insertions and deletions

Different from an array • The size may be changed – Insertions and deletions at the end easily, mostly in constant time – Insertions and deletions in the middle in linear time • Some vectors allow lower bounds to be other than zero – The STL does not • Since it is a class, sometimes bounds checking may be implemented Creative Commons License – Curt Hill.

STL Vectors • Since we largely know what we need to about the general

STL Vectors • Since we largely know what we need to about the general notion of a vector, let’s consider the STL vector • Many methods and topics that are introduced here, will be consistent throughout the STL container classes Creative Commons License – Curt Hill.

Allocators and memory management • When a container class is constructed, memory allocation may

Allocators and memory management • When a container class is constructed, memory allocation may be needed • The STL library has a default allocator for all contained classes that uses the global new and delete • If there is a good reason not to use the global new and delete, then create an allocator Creative Commons License – Curt Hill.

STL default constructors • Most STL classes have no default constructor • Instead, there

STL default constructors • Most STL classes have no default constructor • Instead, there is single parameter constructor with a default parameter – The parameter is the allocator – The default parameter sets the used allocator to be the default allocator • Looks like a default constructor Creative Commons License – Curt Hill.

Aside • Each container class has several features that belong only to it and

Aside • Each container class has several features that belong only to it and several that are common throughout the library • Thus, many of these features seen with the vector will be seen again and again • They might or might not get discussed in the subsequent presentations Creative Commons License – Curt Hill.

Vector • Include is <vector> – A using namespace std; is also required •

Vector • Include is <vector> – A using namespace std; is also required • Constructors provided: – Default – almost – Sized – Copy – Partial copy Creative Commons License – Curt Hill.

Default Constructor • Form: vector<type> vt; • This form requires a default constructor for

Default Constructor • Form: vector<type> vt; • This form requires a default constructor for type • This makes a vector of length zero by default • This a single parameter with a default value – The allocator • Example: vector<int> int_vect; Creative Commons License – Curt Hill.

Sized Constructor • Form: vector<type> vs(int N); • Creates a vector of length N

Sized Constructor • Form: vector<type> vs(int N); • Creates a vector of length N • Example: vector<int> int_vect(j); • Creates a vector that has j entries – Similar to a pointer-based array j does not have to be a constant Creative Commons License – Curt Hill.

Copy Constructor • Form: vector<type> vc(vector<type>) • Makes the new vector the same size

Copy Constructor • Form: vector<type> vc(vector<type>) • Makes the new vector the same size • Applies copy constructor to each element of original • Example: vector<int> int_vect(j); … vector<int> new_vect(int_vect); Creative Commons License – Curt Hill.

Partial copy constructor • Form: vector <type> vc(It 1, It 2) • It 1

Partial copy constructor • Form: vector <type> vc(It 1, It 2) • It 1 and It 2 are input iterators in an existing container class – Recall that iterators are specialized pointers into a STL container class – Does not have to be a vector, but sequentiality has to exist • This constructor then copies from It 1 to It 2 into a new vector – It 1 better be before It 2 Creative Commons License – Curt Hill.

Partial Copy Commentary • This allows us to copy the contents of any container

Partial Copy Commentary • This allows us to copy the contents of any container class into another container class – The two do not have to be the same type of container class but they must both be linear, such as lists, vectors, stacks, etc. – The two must contain the same type of contained primitive or class • We need to see several other things to make this work Creative Commons License – Curt Hill.

Operators • Vectors overloads two operators • The assignment (=) • The reference ([])

Operators • Vectors overloads two operators • The assignment (=) • The reference ([]) Creative Commons License – Curt Hill.

Assignment • May use the = operator – Erases the entire vector and copies

Assignment • May use the = operator – Erases the entire vector and copies in the new one • Example: vector<int> v 1(j), v 2; … v 2 = v 1; • The assign method may also be used as well – Discussion of assign should follow discussion of iterators Creative Commons License – Curt Hill.

Reference • The subscripting brackets may be used: con[i+1] = con[4]+2; – The vector

Reference • The subscripting brackets may be used: con[i+1] = con[4]+2; – The vector and reference may be on either side of an = • at(N) is a function that returns a reference and may also be on either side con[j] = con. at(4) + 2; Creative Commons License – Curt Hill.

Aside • The copy, partial copy and assignment operators will use copy constructor for

Aside • The copy, partial copy and assignment operators will use copy constructor for the data type • If that constructor does a deep copy all is good • If not, then a shallow copy is done • Recall Curt’s rule: – If a class contains pointers or files, then all classes should have default and copy constructor and destructor Creative Commons License – Curt Hill.

Iterators • An iterator is a special pointer • Unlike a regular pointer it

Iterators • An iterator is a special pointer • Unlike a regular pointer it has ++ or -- properly overloaded to give you the next or previous contained item • Vectors are the container class that needs iterators the least Creative Commons License – Curt Hill.

Iterator returning methods • The following methods return an iterator of one type or

Iterator returning methods • The following methods return an iterator of one type or another • begin() – Points to first item in the vector – In general begin is defined for every contain class – It may either be a constant_iterator or an iterator type • end() – Points to the past the end value – Like NULL cannot be dereferenced Creative Commons License – Curt Hill.

Iteration • In general, most containers can be traversed with the following code: Container<TYPE>

Iteration • In general, most containers can be traversed with the following code: Container<TYPE> con; Container<TYPE>: : iterator it; for(it=con. begin(); it!= con. end(); it++) … – We will see this again and again • However, vectors are easier – Just use a for loop and index Creative Commons License – Curt Hill.

Other Member Functions • void clear() – Deletes all elements • int size() –

Other Member Functions • void clear() – Deletes all elements • int size() – Returns the number of elements – Here is how we iterate a vector: for(int j = 0; j<vect. size(); j++) … • These are common to every container class as are others Creative Commons License – Curt Hill.

Capacity • int capacity() • When a vector is allocated, it has a certain

Capacity • int capacity() • When a vector is allocated, it has a certain size • Usually, the capacity is larger – Vector is allocated larger so that insertions may be handled without recopying • capacity() is the number that the vector can grow without a copy – Not an absolute limit • See next slide for picture Creative Commons License – Curt Hill.

Picture Constructor called [0] [1] [2] [3]. . [size-1] used size() capacity() Allocated Constructor

Picture Constructor called [0] [1] [2] [3]. . [size-1] used size() capacity() Allocated Constructor not yet called Creative Commons License – Curt Hill.

Changing size • void push_back(TYPE) – Inserts a new item at end – This

Changing size • void push_back(TYPE) – Inserts a new item at end – This one is easy since no coping of the rest of data is needed • Provided there is a difference between size and capacity • void pop_back() – Deletes the last item – Always easy unless there is no last item • Easy mean O(c) operation Creative Commons License – Curt Hill.

Others • iterator insert(iterator it, TYPE t) – Insert value t before position IT

Others • iterator insert(iterator it, TYPE t) – Insert value t before position IT • Notice that IT is an iterator and not an integer – Returned iterator points at the new t – There are several other inserts as well • One inserts multiple copies of t • Another uses an iterator pair to insert • iterator erase(interator N) – Deletes an element, corresponds to insert Creative Commons License – Curt Hill.

Comparisons • Vectors have the following operators overloaded: – ==, !=, <, >, >=,

Comparisons • Vectors have the following operators overloaded: – ==, !=, <, >, >=, <= • The comparison is lexicographic, like a string • The corresponding elements are compared – The first one that is not equal determines the comparison result • The < is true if every prior element is equal and the first unequal is less than Creative Commons License – Curt Hill. corresponding element in other

Comparison Example A B A B 1 5 4 1 5 5 1 5

Comparison Example A B A B 1 5 4 1 5 5 1 5 6 9 2 9 9 5 1 1 1 5 10 1 1 A<B A <= B A != B A>B A >= B A != B Creative Commons License – Curt Hill. A == B

Others • int max_size() – Maximum capacity – This is a function of available

Others • int max_size() – Maximum capacity – This is a function of available contiguous memory • Of lesser importance: • resize, reserve, front, back, swap, empty Creative Commons License – Curt Hill.

Analysis • Accessing a vector element is O(c) operation – It is a subscript

Analysis • Accessing a vector element is O(c) operation – It is a subscript evaluation, just like an array element • Inserting or deleting an item within the array is O(N) • Adding or removing end item is O(c) or O(N) depending on whether a recopy is needed – Recall difference between size and capacity Creative Commons License – Curt Hill.

Bit vectors • vector<bool> is a specialized type of a regular vector • It

Bit vectors • vector<bool> is a specialized type of a regular vector • It may be stored one bool per bit • The bit vector also supports: flip() – reverses every bit Creative Commons License – Curt Hill.

Conclusion • Vectors are enhanced arrays – Much easier to use – All the

Conclusion • Vectors are enhanced arrays – Much easier to use – All the work of a good dynamic array is done for you • Since vectors are much easier to use, there is no longer a good reason to use arrays Creative Commons License – Curt Hill.