Containers Overview and Class Vector. Container Types Sequence Containers Associative Containers Adapter Classes Stack Container Queue Container Priority Queue Container Set Container Map Container C++ Arrays. Vectors Templated Insertion Sort Template Classes Store Class. STL. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Push elements in any order -- pop operation removes the largest (or smallest) value
18
3
13
15
V alue = 8
27
10
Set ContainersSet ContainersA 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
11
Map ContainersMap ContainersA 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
12
C++ ArraysC++ ArraysFixed-size collection – homogenous Stores the n (size) elements in contiguous block
arr[0 ] arr[1 ] arr[2 ]
0 1 2 n -1
. . . arr[n -1 ]
13
Evaluating an Array as a Evaluating an Array as a ContainerContainer
Size is fixed at the time of its declaration– Does an array know its size?
C++ arrays do not allow the assignment of one array to another– Requires loop structure with the array size as an
upper bound– *or* what algorithm?
14
VectorsVectors
v [ 0 ] v [ 1 ] v [ 2 ] . . . v [ n-1 ] ro o m to gro w
0 1 2 n-1
15
CLASS vector Constructors <vector>
vector ();Create an empty vector. Default.
vector (size_type n, const T& value = T ());
Create a vector with n elements equal to “value”. If the value argument is omitted, the elements are filled with the default value for type T. Type T must have a default constructor, and the default value of type T is specified by the notation T ().
16
CLASS vector Constructors <vector>
vector (InputIter first, InputIter last);Initialize the vector using the range [first, last).
first and last are input iterators (can be read).
17
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.
const T& back () const;
Constant version of back().
bool empty () const;Return true if the vector is empty and false
otherwise.
18
CLASS vector Operations <vector>
T& operator[ ] (size_type 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.
Postcondition: If the operator appears on the left side of an assignment statement, the expression on the right side
modifies the element referenced by the index.
const T& operator[ ] (size_type i) const;Constant version of the index operator.
19
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.
20
CLASS vector Operations <vector>
iterator erase (iterator pos);Erase the element pointed to by pos.Precondition: The vector is not empty.Postcondition: The vector has one fewer element.
iterator erase (iterator first, iterator last);Erase all elements within the iterator range [first,
last).Precondition: The vector is not empty.Postcondition: The size decreases by the
number of elements in the range.
Both invalidate iterators beyond range erased.
21
CLASS vector Operations <vector>
iterator insert (iterator pos, const T& value);Insert value before pos, and return an iterator pointing to the position of the new value in the vector. The
operation invalidates any existing iterators beyond pos.Postcondition: The list has a new element.
22
CLASS vector Operations <vector>
void resize (size_type 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.
void reserve (size_type n);
Only will increase capacity. Postcondition: capacity () >= n
size_type size () const;Return the number of elements in the vector.
23
Output with VectorsOutput with Vectorstemplate <typename T>void writeVector (const vector<T>& v){// capture the size of the vector in nsize_t i, n = v.size ();for (i = 0; i < n; ++i)cout << v[i] << " ";
cout << endl;}
24
Declaring Vector ObjectsDeclaring Vector Objects// vector of size 5 containing the integer // value 2vector<int> intVector (5, 2);
// vector of size 10; each element // contains the empty string vector<string> strVector (10);
25
Adding and Removing Adding and Removing Vector ElementsVector Elements
12 -5 8 1412 -5 8 140
Before
v.size() == 4
43210321
v.size() == 5
After v.push_back (10)
10
4.64.6 6.8
0Before
v.size() == 2
01
v.size() == 1
After v.pop_back ()
26
Resizing a VectorResizing a Vectorint arr[5] = {7, 4, 9, 3, 1};vector<int> v (arr, arr + 5);// v initially has 5 integersv.resize (10);// size is doubledv.resize (4); // vector contracted; data
// is lost
27
Insertion SortInsertion Sort Can use with vector’s or arrays Count comparisons Best-case run time?– T(N) = N - 1 = O(N) Worst-case?
1
1
)(Nj
j
jNT
28
Insertion Sort AlgorithmInsertion Sort Algorithm// sort a vector of type T using insertion // sorttemplate <typename T>void insertionSort (vector<T>& v){// Place v[i] into the sublist v[0] ... //
v[i-1], 1 <= i < n, so it is in the // correct position
size_t n = v.size ();
29
Insertion Sort AlgorithmInsertion Sort Algorithmfor (size_t 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]
size_t j = i; T 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