Vector Arithmetic Operations
vector is designed as a general, flexible container that fits the library's containers, iterators, and algorithms.
The Arithmetic Question
Suppose you have two lists of measurements and want to add their values position by position, or scale every value in one list by the same factor. These are mathematical operations on sequences of numbers. Since vector is a familiar way to hold a list of values, it is natural to ask whether it is also the right tool for that arithmetic. The key is to distinguish a general-purpose container from a type designed with numerical computation in mind.
What do you think happens?
If vector is commonly used to hold a list of values, does that mean it provides mathematical vector operations as part of its design?
Reveal answer
No. vector is designed as a general-purpose container and does not support mathematical vector operations.
Its design emphasizes flexibility and compatibility with containers, iterators, and algorithms, rather than numerical operations and their associated optimizations.
vector in the Library
The standard library’s vector is a broad-purpose mechanism for holding values. Its flexibility makes it useful with many kinds of data and helps it fit the library’s containers, iterators, and algorithms. That role is different from being a mathematical vector type: vector does not provide mathematical vector operations.
The Cost of Generality
It would be easy to imagine adding arithmetic operations to vector. But adding operations alone would not address the deeper design trade-off. The generality and flexibility that make vector useful as a broad container also rule out certain optimizations that are considered essential for serious numerical work. In other words, the limitation is not merely a missing convenience: vector’s general-purpose design constrains the optimizations available for numerical computation.
A Numerical Alternative
For numerical computation, the standard library offers valarray, a vector-like template in the <valarray> header. It is less general than vector and is more amenable to optimization for numerical work. This is a deliberate trade: valarray gives up some of vector’s generality in exchange for a design better suited to numerical computation.
Adding Measurement Lists by Position
Imagine two lists of measurements: the first contains 12 and 15, and the second contains 3 and 4. What does it mean to add these lists position by position?
- 1Pair the first positions
The first value in the first list, 12, is paired with the first value in the second list, 3.
- 2Pair the second positions
The second value in the first list, 15, is paired with the second value in the second list, 4.
- 3Add each pair
The position-by-position sums are 15 and 19.
- 4Connect the task to the design choice
This is an example of the kind of numerical work that motivates a numerical-focused alternative such as valarray. The example illustrates the task; it does not specify C++ operator syntax.
The conceptual result is the list of sums 15 and 19. vector itself does not provide mathematical vector operations.
Mistakes to Avoid
Common mistake Assuming vector provides mathematical vector operations because it holds a list of values.
Why it failsvector is designed as a general-purpose container and does not support mathematical vector operations.
Do this insteadSeparate the job of holding general values from the job of numerical computation.
Common mistake Treating arithmetic operations as the only requirement for serious numerical work.
Why it failsThe generality and flexibility of vector also limit optimizations considered essential for serious numerical work.
Do this insteadConsider the design trade-off, not just whether an operation is available.
Common mistake Looking for valarray in a header other than <valarray>.
Why it failsvalarray is provided in the <valarray> header.
Do this insteadInclude <valarray> when using this template.
Check Your Understanding
A program needs a general-purpose container that works with the standard library’s containers, iterators, and algorithms. Another part of the program is focused on numerical computation. Which design role does each template fit, and which header contains valarray?
Hints
- Recall the design role of vector.
- Compare vector’s generality with valarray’s numerical focus.
- The header name matches the template name.
Key Takeaways
- vector is a flexible, general-purpose container designed to fit with containers, iterators, and algorithms.
- vector does not provide mathematical vector operations.
- Its generality and flexibility limit optimizations considered essential for serious numerical work.
- valarray is a less general, vector-like standard library template aimed at numerical computation and is found in <valarray>.