1 / 45

Main Index

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.

kaori
Télécharger la présentation

Main Index

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 1 Main Index Contents Week 3 – The Vector Container

  2.  

  3. 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. 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.

  5. 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;

  6. 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

  7. CLASS vector <vector> Constructors 7 Main Index Contents http://www.cplusplus.com/reference/vector/vector/

  8. 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);

  9. 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

  10. 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

  11. 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

  12. Adding and Removing Vector Elements

  13. 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

  14. 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

  15. 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; }

  16. 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

  17. Insertion Sort • Animation • Pseudo-code

  18. Insertion Sort • "swap" operation in-place as  temp ← A[j]; A[j] ← A[j-1]; A[j-1] ← temp;

  19. 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

  20. 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

  21. 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; } }

  22. Time Complexity (Big O)

  23. 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)).

  24. Container classes Underlying storage structure The storage structure should be selected so that the container operations can be implemented efficiently

  25. 36 Main Index Contents Container Types

  26. Details on STL Containers http://www.cplusplus.com/reference/stl/

  27. 38 Main Index Contents Sequence Containers A sequence container stores data by position in linear order 1st element, 2nd element, and so forth.

  28. Inserting into a List Container 39 Main Index Contents The List Container

  29. 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.

  30. 41 Main Index Contents Stack Containers (LIFO) A stack allows access at only one end of the sequence, called the top.

  31. 42 Main Index Contents Queue Containers (FIFO) A queue is a container that allows access only at the front and rear of the sequence.

  32. 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.

  33. 44 Main Index Contents Set Containers A set is a collection of unique values, called keys or set members.

  34. 45 Main Index Contents Map Containers A map is a storage structure that implements a key-value relationship.

More Related