Professional C__ - Marc Gregoire [193]
C++11 adds a new constructor to the vector class that accepts an initializer_list that contains the initial elements for the vector. It can be used as follows:
vector Code snippet from VectorCtors\intializer_list.cpp initializer_lists can also be used for so-called uniform initialization as discussed in Chapter 9. Uniform initialization works on most STL containers. For example, the following code demonstrates this for a vector: vector Code snippet from VectorCtors\UniformInitialization.cpp The vector stores copies of the objects, and its destructor calls the destructor for each of the objects. You can allocate vectors on the heap as well: vector delete elementVector; Code snippet from VectorCtors\HeapVectors.cpp Remember to call delete when you are finished with a vector that you allocated with new or better yet, use a smart pointer to automatically deallocate the vector as follows: shared_ptr Code snippet from VectorCtors\HeapVectorsSmartPointer.cpp Use delete, not delete[], to free vectors, because a vector is a basic type and not an array type. Copying and Assigning vectors The copy constructor and assignment operator for the vector class perform deep copies of all the elements in the vector. Thus, for efficiency, you should pass vectors by reference or const reference to functions and methods. Consult Chapter 19 for the details on writing functions that take template instantiations as parameters. In addition to normal copying and assignment, vectors provide an assign() method that removes all the current elements and adds any number of new elements. This method is useful if you want to reuse a vector. Here is a trivial example. intVector is created with 10 elements with value 0. Then assign() is used to remove all 10 elements and replace them with 5 elements with value 100. vector // Other code . . . intVector.assign(5, 100); Code snippet from VectorCopyAssign\demo.cpp With C++11, assign() can also accept an initializer_list as follows. intVector will now have 4 elements with the given values. intVector.assign({1, 2, 3, 4}); Code snippet from VectorCopyAssign\demo.cpp vectors also provide a swap() method that allows you to swap the contents of two vectors. Here is a simple example: vector vector vectorOne.swap(vectorTwo); // vectorOne now has 5 elements with the value 100. // vectorTwo now has 10 elements with the value 0. Code snippet from VectorCopyAssign\demo.cpp Comparing vectors The STL provides the usual six overloaded comparison operators for vectors: ==, !=, <, >, <=, >=. Two vectors are equal if they have the same number of elements and all the corresponding elements in the two vectors are equal to each other. One vector is “less than” another if all elements 0 through i-1 in the first vector are equal to 0 through i-1 in the second vector, but element i in the first is less than element i in the second, where i must be in the range 0...n and n must be <= size()-1. Comparing two vectors with operator== or operator!= requires the individual elements to be comparable with operator==. Comparing two vectors with operator<, operator>, operator<=, or operator>= requires the individual elements to be comparable with operator<. If you intend to store objects of a custom class in a vector, make sure to write those operators. Here is an example of a simple program that compares vectors of ints: vector vector if (vectorOne == vectorTwo) { cout << "equal!" << endl; } else { cout << "not equal!" << endl; } vectorOne[3] = 50; if (vectorOne < vectorTwo) { cout << "vectorOne is less than vectorTwo" << endl; } else { cout << "vectorOne is not less than vectorTwo" << endl; } Code snippet from VectorCompare\compare.cpp The output of