The Vector Container A Container is a class that stores a collection of data. Its operations allow the programmer to insert, erase, and update elements in the collection. The Standard Template Library (STL) includes most of the classic data structures that programmers use The C++ template mechanism allows us to declare generic classes. The objects of generic classes can store data of different types. The C++ template mechanism allows a programmer to abstract the data type on which the algorithm operates from the algorithm itself. The STL Library constists of 10 container classes within 3 catagories. The classes are categorized according to the ordering of the elements and the different types of operations that access the data. The three categories are: Sequence Containers Adapter Containers Associative Containers vector deque list stack queue priority_queue set, multiset map, multimap The vector container is a generalized array that stores a collection of elements of the same data type. Like an array, the data is accessed using an index. However, unlike an array the size of the vector can grow and contract dynamically at the rear. A vector object allows the use of an index operator. For example: Vector v ……. 0 1 2 3 4 5 v[5] = 15; // assigns integer 15 to the vector at index 5 value = v[5] * 2; Declaring Vector Objects There are three constructors of the vector class that can be used create and initialize a vector vector<int> intVector; // no argument – vector is 0 length vector<int> intVector(5); // vector of size 5 containing the integer value 0 vector<string> strVector(10); // vector of size 10; each element contains an // empty string By declaring a vector using only the size the declaration is equivalent to declaring an array. Ex: int intArray[5]; str strArray[10]; The following example declares and a vector containing 80 blank characters: vector<char> line( 80, ‘ ‘ ); The following example declares a vector of t time24 objects: vector<time24> openTime( 7, time24(9,0) ); time24 constructor will be called 7 times to initialize each object Braces cannot be used to initialize a vector. But the following is legal: int intArray[5] = { 9, 2, 7, 3, 12 }; vector<int> intVector(intArray, intArray+5); passing the address of the array, base address and base address + 5. The compiler knows the size of any data type. For example, using the unary sizeof operator and giving it the data type or object as argument, the operator returns the number of bytes required to store the data. int x = sizeof(char); // number of bytes returned as an integer value If you do not know the size of the array it is always possible to calculate the size as shown in the following example: int intArray[] = { 9, 2, 7, 3, 12 }; int arrSize = sizeof(intArray)/sizeof(int); vector<int> intVector(intArray, intArray+ arrSize); // File: prg4_2.cpp // the program generates 12 random integers in the range 100 to 999. Add each number // value to vector vSmall if value < 400, to vector vMedium if 400 <= value < 700 // and to vLarge if 700 <= value < 1000. Apply the insertion sort to order each vector. // Use join() so that vSmall is the concatenation of the three vectors. Sort the final list // in vSmall by using the insertion sort. display the initial set of elements in each vector // and the final sorted list by calling writeVector() #include <iostream> #include <vector> #include "d_random.h" #include "d_sort.h" #include "d_util.h" // for randomNumber // for insertionSort() // for writeVector() using namespace std; // attach vB onto the end of vA template <typename T> void join (vector<T> &vA, const vector<T>& vB); int main() { // declare 3 integer vectors vector<int> vSmall, vMedium, vLarge; // use a random number generator randomNumber rnd; int i, value; for (i = 0; i < 12; i++) { value = rnd.random(900) + 100; if (value < 400) vSmall.push_back(value); else if (value < 700 ) vMedium.push_back(value); else vLarge.push_back(value); } // sort the vector of integers in the range 100 <= n < 400 and output insertionSort(vSmall); cout << "Small: "; writeVector(vSmall); // sort the vector of integers in the range 400 <= n < 700 and output insertionSort(vMedium); cout << "Medium: "; writeVector(vMedium); // sort the vector of integers in the range 700 <= n < 1000 and output insertionSort(vLarge); cout << "Large: "; writeVector(vLarge); // join vMedium onto the end of vSmall join(vSmall, vMedium); // join vLarge onto the end of the modified vSmall join(vSmall, vLarge); // output the new vector cout << "Sorted: "; writeVector(vSmall); return 0; } template <typename T> void join (vector<T> &vA, const vector<T>& vB) { // capture the size of vB in sizeB int i, sizeB = vB.size(); // use index i to access the elements of vB and push_back() // to add the elements to the rear of vA for (i = 0; i < sizeB; i++) vA.push_back(vB[i]); } /*Run: Small: 176 213 269 287 357 Medium: 482 535 551 649 687 Large: 843 901 Sorted: 176 213 269 287 357 482 535 551 649 687 843 901 */