Professional C__ - Marc Gregoire [192]
doubleVector[i] /= max;
cout << doubleVector[i] << " ";
}
Code snippet from TestScores\TestScores.cpp
As you can see from this example, you can use a vector just as you would use a standard C-style array.
The operator[] on a vector normally returns a reference to the element, which can be used on the left-hand side of assignment statements. If operator[] is called on a const vector object, it returns a reference to a const element, which cannot be used as the target of an assignment. See Chapter 18 for details on how this trick is implemented.
Specifying an Initial Element Value
You can specify an initial value for the elements when you create the vector like this:
vector // Initialize max to smallest number double max = numeric_limits // No need to initialize each element: the constructor did it for you. for (size_t i = 0; i < 10; i++) { cout << "Enter score " << i + 1 << ": "; cin >> doubleVector[i]; if (doubleVector[i] > max) { max = doubleVector[i]; } } max /= 100.0; for (size_t i = 0; i < 10; i++) { doubleVector[i] /= max; cout << doubleVector[i] << " "; } Code snippet from TestScores\TestScoresInitialElem.cpp Other vector Element Access Methods In addition to using operator[], you can access vector elements via at(), front(), and back(). The at() method is identical to operator[], except that it performs bounds checking, and throws an out_of_range exception if the index is out of bounds. front() and back() return the references to the first and last elements of the vector, respectively. All vector element accesses run in constant complexity. Dynamic-Length vectors The real power of the vector lies in its ability to grow dynamically. For example, consider the test score normalization program from the previous section with the additional requirement that it should handle any number of test scores. Here is the new version: vector // Initialize max to smallest number double max = numeric_limits for (size_t i = 0; true; i++) { double temp; cout << "Enter score " << i + 1 << " (-1 to stop): "; cin >> temp; if (temp == -1) { break; } doubleVector.push_back(temp); if (temp > max) { max = temp; } } max /= 100.0; for (size_t i = 0; i < doubleVector.size(); i++) { doubleVector[i] /= max; cout << doubleVector[i] << " "; } Code snippet from TestScores\TestScoresDynamic.cpp This version of the program uses the default constructor to create a vector with zero elements. As each score is read, it’s added to the vector with the push_back() method, which takes care of allocating space for the new element. Note that the last for loop uses the size() method on the vector to determine the number of elements in the container. size() returns an unsigned integer, so the type of i size_t. vector Details Now that you’ve had a taste of vectors, it’s time to delve into their details. Constructors and Destructors The default constructor creates a vector with 0 elements. vector Code snippet from VectorCtors\DefaultCtor.cpp As you’ve already seen, you can specify a number of elements and, optionally, a value for those elements, like this: vector Code snippet from VectorCtors\InitialElements.cpp If you omit the default value, the new objects are zero-initialized. Zero-initialization constructs objects with the default constructor and initializes primitive integer types (such as char, int, etc.) to 0 and primitive floating point types to 0.0. You can also create vectors of built-in classes like this: vector Code snippet from VectorCtors\BuiltInClasses.cpp User-defined classes can also be used as vector elements: class Element { public: Element() {} virtual ~Element() {} }; int main() { vector return 0; } Code snippet