Why is a C++ Vector called a Vector?

后端 未结 16 2375
情话喂你
情话喂你 2020-12-04 07:42

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.

16条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 07:50

    Think of a C++ vector as a dynamic array, which size can be altered by inserting or removing elements. They are not related to the vector's mathematical definition.

    Vectors in Mathematics

    Consider an nxm matrix called A, where n corresponds to the number of rows, and m corresponds to the number of columns. In a mathematical context, once you introduce a matrix like this, then later, you can't do any operations outside of A's range and you can't extend A's size either. What this means is you can't refer to an index of [n + 1] and/or [m + 1].

    Now, a vector of A derives these attributes as well, while their dimensions will always be 1xm (any [i] row selected within A) or nx1 (any [j] column selected within A). A vector also cannot be specified as 2xn, because a collection of vectors cannot be interpreted as one vector, while one vector - let that be the [i] column vector of A with the dimensions of 1xm - can be interpreted as a matrix.

    The important takeaway is that you cannot change the dimensions of a vector once it is introduced in terms of mathematics.

    Vectors in C++

    In C++, vectors are just like vectors in mathematics, but unlike in mathematics their size can be altered. Size as a term applies here because it implies the element count that one particular vector contains.

    You use the term dimensions in terms of C++ vectors, when you have a vector of vectors: std::vector>> ragged_array. In this example, I called that vector "ragged", because it demonstrates how the size of each vector of that vector can be altered independently. It not only violates the rules of how the dimensions cannot be changed once a particular vector is introduced in mathematics, but it also demonstrates, how it cannot be used as a matrix.

提交回复
热议问题