450 likes | 638 Views
1. Main Index. Contents. Week 3 – The Vector Container. . . . 3. Main Index. Contents. C++ Arrays. An array is a fixed-size collection of values of the same data type. An array is a container that stores the n (size) elements in a contiguous block of memory.
E N D
1 Main Index Contents Week 3 – The Vector Container
3 Main Index Contents C++ Arrays An array is a fixed-size collection of values of the same data type. An array is a container that stores the n (size) elements in a contiguous block of memory. intarr[] = {1,2,3,4,5}; cout << arr[4];
4 Main Index Contents Evaluating an Array as a Container • The size of an array is fixed at the time of its declaration and cannot be changed during the runtime. • An array cannot report its size. A separate integer variable is required in order to keep track of its size. • C++ arrays do not allow the assignment of one array to another. • The copying of an array requires the generation of a loop structure with the array size as an upper bound.
Variable-sized array • Use dynamically allocated memory to determine the size of the array at run time • Programmers need to handle array resize manually • STL makes this easier int h, *b; cin >> h; b = new int[h]; ... delete [] b;
Allow for dynamic resizing Have a way to store the size internally Allow for assignment of one object to another 6 Main Index Contents Vectors • A container is a class that stores a collection of data • It has operations that allow a programmer to insert, • erase, and update elements in the collection
CLASS vector <vector> Constructors 7 Main Index Contents http://www.cplusplus.com/reference/vector/vector/
Declaring Vector Objects // vector of size 5 containing the integer // value 0 vector<int> intVector(5); // After assigning value for each element: // vector of size 10; each element // contains the empty string vector<string> strVector(10);
CLASS vector <vector> Operations T& back(); Return the value of the item at the rear of the vector. Precondition: The vector must contain at least one element. bool empty() const; Return true if the vector is empty and false otherwise. 9 Main Index Contents
CLASS vector <vector> Operations T& operator[] (int 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. 10 Main Index Contents
CLASS vector <vector> Operations 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. 11 Main Index Contents
CLASS vector <vector> Operations void resize((int 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. int size() const; Return the number of elements in the vector. 13 Main Index Contents
Resizing a Vector int arr[5] = {7, 4, 9, 3, 1}; vector<int> v(arr,arr+5); // v initially has 5 integers v.resize(10);// list size is doubled v.resize(4); // list is contracted. data // is lost
Output with Vectors // number of elements in list is v.size() template <typename T> void writeVector(const vector<T>& v) { // capture the size of the vector in n int i, n = v.size(); for(i = 0; i < n; i++) cout << v[i] << " "; cout << endl; }
C++ interview questions on “Vector” What do vectors represent?a) Static arraysb) Dynamic arraysc) Stackd) Queue Answer: bExplanation: Vectors are sequence containers representing arrays that can change in size. More questions here
Insertion Sort • Animation • Pseudo-code
Insertion Sort • "swap" operation in-place as temp ← A[j]; A[j] ← A[j-1]; A[j-1] ← temp;
30 Main Index Contents Insertion Sort Implementation insertionSort(): // sort a vector of type T using insertion // sort template <typename T> void insertionSort(vector<T>& v) { int i, j, n = v.size(); T temp; // place v[i] into the sublist v[0] ... // v[i-1], 1 <= i < n, so it is in the // correct position
31 Main Index Contents Insertion Sort Implementation for (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] j = i; 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
32 Main Index Contents Insertion Sort Implementation while (j > 0 && temp < v[j-1]) { // shift elements up list to make // room for insertion v[j] = v[j-1]; j--; } // the location is found; insert temp v[j] = temp; } }
Worst case • Best case: input is an array that is already sorted. Linear running time (i.e., Θ(n)). • Worst case: input is an array sorted in reverse order. Quadratic running time (i.e., O(n2)).
Container classes Underlying storage structure The storage structure should be selected so that the container operations can be implemented efficiently
36 Main Index Contents Container Types
Details on STL Containers http://www.cplusplus.com/reference/stl/
38 Main Index Contents Sequence Containers A sequence container stores data by position in linear order 1st element, 2nd element, and so forth.
Inserting into a List Container 39 Main Index Contents The List Container
Adapter Classes • An adapter contains a sequence container as its underlying storage structure. • The programmer interface for an adapter provides only a restricted set of operations from the underlying storage structure.
41 Main Index Contents Stack Containers (LIFO) A stack allows access at only one end of the sequence, called the top.
42 Main Index Contents Queue Containers (FIFO) A queue is a container that allows access only at the front and rear of the sequence.
43 Main Index Contents Associative Containers • Associative containers store elements by key. • Ex: name, social security number, or part number. • A program accesses an element in an associative container by its key, which may bear no relationship to the location of the element in the container.
44 Main Index Contents Set Containers A set is a collection of unique values, called keys or set members.
45 Main Index Contents Map Containers A map is a storage structure that implements a key-value relationship.