Concepts / Vector Arithmetic Operations

Vector Arithmetic Operations

vector is designed as a general, flexible container that fits the library's containers, iterators, and algorithms.

  • Programming

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?

  • Yes. Holding a list of values means it is designed for mathematical vector arithmetic.

  • No. It is designed as a general-purpose container.

Reveal answer
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.

vectorContainersGeneral-purpose useIteratorsAlgorithmsfits the library offits withfits withdesigned for
How does vector fit with the library’s containers, iterators, and algorithms? The diagram shows these design relationships, not a claim that vector provides 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.

General, flexibledesignBroad-purpose useSerious numericalworkOptimizationconstraintssupportslimits somerestricts optimizations for
Why can adding arithmetic operations to vector fail to solve the needs of serious numerical work? This is a design-level relationship, not a diagram of a particular physical memory arrangement.

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.

vectorGeneral containerNo mathematicalvector operationsvalarrayNumerical computationLess general, moreamenable tooptimization<valarray>designed asdoes not provideaimed atisdeclared in
How do the two templates differ in purpose and design? Both can hold values, but their stated design roles differ.
Worked example

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?

  1. 1
    Pair the first positions

    The first value in the first list, 12, is paired with the first value in the second list, 3.

  2. 2
    Pair the second positions

    The second value in the first list, 15, is paired with the second value in the second list, 4.

  3. 3
    Add each pair

    The position-by-position sums are 15 and 19.

  4. 4
    Connect 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.

Result

The conceptual result is the list of sums 15 and 19. vector itself does not provide mathematical vector operations.

List A, position 012List B, position 03Sum, position 015List A, position 115List B, position 14Sum, position 119paired withpaired withpaired withpaired with
When two lists of measurements are added by position, which values are paired? Each position in one list pairs with the same position in the other. The values illustrate the task, not a claim about specific C++ operator syntax.

Mistakes to Avoid

  • Common mistake

    Assuming vector provides mathematical vector operations because it holds a list of values.

    Why it fails

    vector is designed as a general-purpose container and does not support mathematical vector operations.

    Do this instead

    Separate 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 fails

    The generality and flexibility of vector also limit optimizations considered essential for serious numerical work.

    Do this instead

    Consider the design trade-off, not just whether an operation is available.

  • Common mistake

    Looking for valarray in a header other than <valarray>.

    Why it fails

    valarray is provided in the <valarray> header.

    Do this instead

    Include <valarray> when using this template.

Check Your Understanding

EASY

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>.

Related concepts

More concepts starting with V