Online Book Reader

Home Category

Professional C__ - Marc Gregoire [191]

By Root 1177 0
on a vector object, which does not copy or move anything. Instead, it makes space in the container and constructs the object in place. Following is an example.

vec.emplace_back(12, "Twelve");

The emplace methods take a variable number of arguments as a variadic template. Variadic templates are discussed in Chapter 20. The difference in performance between emplace_back() and push_back() using move semantics depends on how your specific compiler implements these operations. In most situations you can pick the one based on the syntax that you prefer.

vec.push_back({12, "Twelve"});

// Or

vec.emplace_back(12, "Twelve");

SEQUENTIAL CONTAINERS


The vector, deque, list, array, and forward_list are called the sequential containers. The best way to learn about the sequential containers is to jump in with an example of the vector, which is the container most commonly used. The next section describes the vector in detail as an example of a sequential container, followed by briefer descriptions of the deque, list, array, and forward_list. Once you become familiar with the sequential containers, it’s trivial to switch between them.

vector

The STL vector is similar to a standard C-style array: The elements are stored in contiguous memory, each in its own “slot.” You can index into the vector, as well as add new elements to the back or insert them anywhere else. Inserting and deleting elements into and from the vector generally takes linear time, though these operations actually run in amortized constant time at the end of the vector, explained in the section “The vector Memory Allocation Scheme” later in this chapter. Random access of individual elements has a constant complexity.

vector Overview

The vector is defined in the header file as a class template with two type parameters: the element type to store and an allocator type.

template > class vector;

The Allocator parameter specifies the type for a memory allocator object that the client can set in order to use custom memory allocation. This template parameter has a default value.

The default value for the Allocator type parameter is sufficient for most applications. Programmers do not usually find it useful to customize allocators, but Chapter 17 provides more details in case you are interested. This chapter assumes that you always use the default allocator.

Fixed-Length vectors

The simplest way to use a vector is as a fixed-length array. The vector provides a constructor that allows you to specify the number of elements, and provides an overloaded operator[] in order to access and modify those elements. The C++ standard states that the result of operator[] is undefined when used to access an element outside the vector bounds. This means that any compiler can decide how to behave in that case. For example, Microsoft Visual C++ will give a run time error message when your program is compiled in debug mode. In release mode, those checks are disabled for performance reasons. Note that the standard does provide an at() method which will perform bounds checking and is discussed later in this chapter.

Like “real” array indexing, the operator[] on a vector does not provide bounds checking.

For example, here is a small program to “normalize” test scores so that the highest score is set to 100, and all other scores are adjusted accordingly. The program creates a vector of 10 doubles, initializes all elements to 0.0, reads in 10 values from the user, divides each value by the max score (times 100), and prints out the new values. For the sake of brevity, the program forsakes error checking.

vector doubleVector(10); // Create a vector of 10 doubles.

// Initialize max to smallest number

double max = numeric_limits::lowest();

for (size_t i = 0; i < 10; i++) {

doubleVector[i] = 0.0;

}

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++) {

Return Main Page Previous Page Next Page

®Online Book Reader