Why is a C++ Vector called a Vector?

Viewed 37652

The question's pretty self-explanatory really. I know vaguely about vectors in maths, but I don't really see the link to C++ vectors.

18 Answers

It's called a vector because Alex Stepanov, the designer of the Standard Template Library, was looking for a name to distinguish it from built-in arrays. He admits now that he made a mistake, because mathematics already uses the term 'vector' for a fixed-length sequence of numbers. C++11 compounds this mistake by introducing a class 'array' that behaves similarly to a mathematical vector.

Alex's lesson: be very careful every time you name something.

Mathematical definition of a vector is a member of the set Sn, which is an ordered sequence of values in a specific set (S). This is what a C++ vector stores.

An excerpt from The C++ Programming Language by Bjarne Stroustrup:

"One could argue that valarray should have been called vector because it is a traditional mathematical vector and that vector should have been called array. However, this is not the way the terminology evolved."

The name comes from the linear algebra, where vector is matrix with only one column or only one row.

Just to say why it probably isn't called array: Because std::vector has a dynamic size. An array conceptually is fixed in length. Next C++ Standard by the way has a std::array template, which is fixed in size and should be preferred over a plain array:

std::array<int, 4> f = { 1, 2, 3, 4 };

A vector is simply a sequence of values, all of the same type. This is pretty much in line with the use in mathematics. I guess the mathematical idea that vectors should support some common operations (such as adding, and scaling by a scalar) are not carried over, the important aspect is mainly the structure.

Also if you make it store integers or floating points it does make an excellent type for storing N dimensional vectors. After all all a vector is, is a list of numbers kept in a specific order.

I'd guess it comes from the term row vector. Also, computer scientists love thinking up new names for things...

No idea about the real reason, but C++ calling it a vector instead of an array, reduces confusion between the C and C++ structures, although they fulfill the same roles.

Wonders that parametrisation on types does to names..

here a column gets blasted.. (view source for some server-side ASP.NET HTML encoding skills)

or was it a row?

Then again, thinking of it in MIMD or even SSE vector machine context, the name still sounds damn good.

To complement the answer of Mark Ruzon, here are the words from Alex Stepanov in his 2015 book, From Mathematics to Generic Programming (w/ Daniel Rose):

The name vector in STL was taken from the earlier programming languages Scheme and Common Lisp. Unfortunately, this was inconsistent with the much older meaning of the term in mathematics … this data structure should have been called array. Sadly, if you make a mistake … the result might stay around for a long time.

There are quite a few good negative answers, i.e. vector is not a good name. I would like to add more information why it is called so, and how the word vector is used in the computing industry.

Vector means ‘carrier’ literally. However, the main sense derives from mathematical usage, meaning:

  1. directed quantity (earlier 19th century)
  2. ordered set of numbers (later 19th century)

The second sense is extended to computing, and we have ‘a sequence of consecutive locations in memory’ (Shorter Oxford English Dictionary). It has a similar meaning to array, and we have terms like:

  • vector processor
  • vector instructions
  • vectorization
  • initialization vector
  • interrupt vector

They are established usages, so the C++ use of vector is not wrong. However, as Mr Stepanov put it (in From Mathematics to Generic Programming, when he talked about naming principles):

If there are conflicting usages, the much more established one wins.

His regret was really that this use of vector conflicted with the (more established) mathematical usage, and it could have been better to avoid it in the beginning.

it comes from the structure of matrix which build from vectors

Related