Download presentation
Presentation is loading. Please wait.
Published byClement Davis Modified over 9 years ago
1
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container
2
Main Index Contents 2
3
Main Index Contents 33 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. int arr[] = {1,2,3,4,5}; cout << arr[4]; int arr[] = {1,2,3,4,5}; cout << arr[4];
4
Main Index Contents 44 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.
5
Main Index Contents 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 STL 5 int h, *b; cin >> h; b = new int[h];... delete [] b;
6
Main Index Contents 6 1.Allow for dynamic resizing 2.Have a way to store the size internally 3.Allow for assignment of one object to another 6 Main Index Contents Vectors A container is a class that stores a collection of datacontainer It has operations that allow a programmer to insert, erase, and update elements in the collection A container is a class that stores a collection of datacontainer It has operations that allow a programmer to insert, erase, and update elements in the collection
7
Main Index Contents 77 Main Index Contents CLASS vector Constructors http://www.cplusplus.com/reference/vector/vector/
8
Main Index Contents 8 Declaring Vector Objects // vector of size 5 containing the integer // value 0 vector intVector(5); // After assigning value for each element: // vector of size 10; each element // contains the empty string vector strVector(10);
9
Main Index Contents 99 Main Index Contents CLASS 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.
10
Main Index Contents 10 Main Index Contents CLASS 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.
11
Main Index Contents 11 Main Index Contents CLASS 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.
12
Main Index Contents 12 Adding and Removing Vector Elements
13
Main Index Contents 13 Main Index Contents CLASS 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.
14
Main Index Contents 14 Resizing a Vector int arr[5] = {7, 4, 9, 3, 1}; vector 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
15
Main Index Contents 15 Output with Vectors // number of elements in list is v.size() template void writeVector(const vector & 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; }
16
Main Index Contents C++ interview questions on “Vector” 16 What do vectors represent? a) Static arrays b) Dynamic arrays c) Stack d) Queue Answer: b Explanation: Vectors are sequence containers representing arrays that can change in size. More questions here
17
Main Index Contents 17
18
Main Index Contents 18
19
Main Index Contents 19
20
Main Index Contents 20
21
Main Index Contents 21
22
Main Index Contents 22
23
Main Index Contents 23
24
Main Index Contents 24
25
Main Index Contents 25
26
Main Index Contents 26
27
Main Index Contents 27
28
Main Index Contents Insertion Sort Animation Pseudo-code 28
29
Main Index Contents Insertion Sort "swap" operation in-place as temp ← A[j]; A[j] ← A[j-1]; A[j-1] ← temp; 29
30
Main Index Contents 30 Main Index Contents Insertion Sort Implementation insertionSort(): // sort a vector of type T using insertion // sort template void insertionSort(vector & 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 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 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; }
33
Main Index Contents Time Complexity (Big O) 33
34
Main Index Contents 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(n 2 )). 34
35
Main Index Contents 35 Underlying storage structure Container classes The storage structure should be selected so that the container operations can be implemented efficiently
36
Main Index Contents 36 Main Index Contents Container Types Sequence Containers Adapter Containers Associative Containers VectorStackSet, Multiset DequeQueue Map, Mutltimap ListPriority Queue
37
Main Index Contents Details on STL Containers 37 http://www.cplusplus.com/reference/stl/
38
Main Index Contents 38 Main Index Contents Sequence Containers A sequence container stores data by position in linear order 1st element, 2nd element, and so forth.
39
Main Index Contents 39 Main Index Contents The List Container Inserting into a List Container
40
Main Index Contents 40 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 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 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 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 44 Main Index Contents Set Containers A set is a collection of unique values, called keys or set members.
45
Main Index Contents 45 Main Index Contents Map Containers A map is a storage structure that implements a key- value relationship.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.