Containers Overview and Class Vector Container Types Sequence

















![CLASS vector Operations <vector> T& operator[ ] (size_type i); Allow the vector element at CLASS vector Operations <vector> T& operator[ ] (size_type i); Allow the vector element at](https://slidetodoc.com/presentation_image_h/957d3501e5e079dbae82037005e01e5b/image-18.jpg)







![Resizing a Vector int arr[5] = {7, 4, 9, 3, 1}; vector<int> v (arr, Resizing a Vector int arr[5] = {7, 4, 9, 3, 1}; vector<int> v (arr,](https://slidetodoc.com/presentation_image_h/957d3501e5e079dbae82037005e01e5b/image-26.jpg)



![Insertion Sort Algorithm while (j > 0 && temp < v[j-1]) { // Shift Insertion Sort Algorithm while (j > 0 && temp < v[j-1]) { // Shift](https://slidetodoc.com/presentation_image_h/957d3501e5e079dbae82037005e01e5b/image-30.jpg)



- Slides: 33

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 1 Vectors Templated Insertion Sort Template Classes Store Class

l l STL C++ STL provides 10 container classes 4 additional types considered “near-containers” – – l Extensions to Standard – – – 2 array string bitset valarray slist rope – huge strings unordered_set, unordered_map, unordered_multiset, unordered_multimap

Container Categories 3 Sequence Containers Adapter Containers Associative Containers Vector Stack Set, Multiset Deque Queue Map, Multimap List Priority Queue

Sequence Containers A sequence container stores data by position in linear order 1 st element, 2 nd element, and so forth 4

l Associative Containers Associative containers store elements by key – l A program accesses an element in an associative container by its key – – l 5 Ex: name, SS #, or part number May bear no relationship to the location of the element in the container Access by key O(lg N) Implementation?

l Adapter Classes Use a sequence container as underlying storage structure – – l Adapter’s interface provides a restricted set of operations from underlying storage structure – – 6 E. g. Stack and Queue use Deque Design Pattern: Adapter How is stack restricted? queue?

Stack Containers A stack allows access at only one end of sequence, called the top. C A Push A 7 top B A Push B (a) top Push C B A top Pop C C B A B top A Pop B (b) top

Queue Containers A queue is a container that allows access only at front and back A B C Insert 8 B D E A back front C Delete D E

Priority Queue Containers priority queue -- HPFO Push elements in any order -- pop operation removes the largest (or smallest) value 9

Set Containers A set is a collection of unique values, called keys or set members. 10

Map Containers A map is a storage structure that implements a key-value relationship. 11

C++ Arrays Fixed-size collection – homogenous Stores the n (size) elements in contiguous block 12

l Evaluating an Array as a Container Size is fixed at the time of its declaration – l C++ arrays do not allow the assignment of one array to another – – 13 Does an array know its size? Requires loop structure with the array size as an upper bound *or* what algorithm?

Vectors 14

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 (). 15

CLASS vector Constructors <vector> vector (Input. Iter first, Input. Iter last); Initialize the vector using the range [first, last). first and last are input iterators (can be read). 16

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. 17
![CLASS vector Operations vector T operator sizetype i Allow the vector element at CLASS vector Operations <vector> T& operator[ ] (size_type i); Allow the vector element at](https://slidetodoc.com/presentation_image_h/957d3501e5e079dbae82037005e01e5b/image-18.jpg)
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. 18

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

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

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

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

Output with Vectors template <typename T> void write. Vector (const vector<T>& v) { // capture the size of the vector in n size_t i, n = v. size (); for (i = 0; i < n; ++i) cout << v[i] << " "; cout << endl; } 23

Declaring Vector Objects // vector of size 5 containing the integer // value 2 vector<int> int. Vector (5, 2); // vector of size 10; each element // contains the empty string vector<string> str. Vector (10); 24

Adding and Removing Vector Elements v. size() == 5 v. size() == 4 12 -5 0 1 8 2 Before v. size() == 2 4. 6 6. 8 0 1 Before 25 14 3 12 -5 0 1 8 2 14 3 10 4 After v. push_back (10) v. size() == 1 4. 6 0 After v. pop_back ()
![Resizing a Vector int arr5 7 4 9 3 1 vectorint v arr Resizing a Vector int arr[5] = {7, 4, 9, 3, 1}; vector<int> v (arr,](https://slidetodoc.com/presentation_image_h/957d3501e5e079dbae82037005e01e5b/image-26.jpg)
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); // size is doubled v. resize (4); // vector contracted; data // is lost 26

l l l Insertion Sort Can use with vector’s or arrays Count comparisons Best-case run time? – l 27 T(N) = N - 1 = O(N) Worst-case?

Insertion Sort Algorithm // sort a vector of type T using insertion // sort template <typename T> void insertion. Sort (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 (); 28 //

Insertion Sort Algorithm for (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 29
![Insertion Sort Algorithm while j 0 temp vj1 Shift Insertion Sort Algorithm while (j > 0 && temp < v[j-1]) { // Shift](https://slidetodoc.com/presentation_image_h/957d3501e5e079dbae82037005e01e5b/image-30.jpg)
Insertion Sort Algorithm while (j > 0 && temp < v[j-1]) { // Shift elements up list to make // room for insertion v[j] = v[j-1]; --j; } // Location is found; insert temp v[j] = temp; } } 30

Class Templates l Can templatize classes as well as functions l All container classes in STL are templates l Syntax template <typename T 1, typename T 2, … > class Class. Name { … } l 31 Concrete Store <T> template example

Class Template Store template <typename T> class Store { public: // Ctor: init w/item or default T Store (const T& item = T()) : m_value (item) { } 32 T get. Value () const { return m_value; }

Class Template Store (Cont’d) void set. Value (const T& item) { m_value = item; } private: T m_value; }; Use: Store<double> s; s. set. Value (4. 3); 33