为什么将 C++ Vector 称为 Vector?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/581426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is a C++ Vector called a Vector?
提问by Skilldrick
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.
这个问题真的不言自明。我对数学中的向量知之甚少,但我并没有真正看到 C++ 向量的链接。
采纳答案by Mehrdad Afshari
Mathematical definition of a vector is a member of the set S
n
, which is an ordered sequence of values in a specific set (S
). This is what a C++ vector
stores.
向量的数学定义是集合的成员,集合S
n
是特定集合 ( S
)中值的有序序列。这就是 C++vector
存储的内容。
回答by Mark Ruzon
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. Now C++0X will compound this mistake by introducing a class 'array' that will behave similar to a mathematical vector.
之所以称为向量,是因为标准模板库的设计者 Alex Stepanov 正在寻找一个名称以将其与内置数组区分开来。他现在承认他犯了一个错误,因为数学已经使用术语“向量”来表示固定长度的数字序列。现在 C++0X 将通过引入一个类似于数学向量的类“数组”来加剧这个错误。
Alex's lesson: be very careful every time you name something.
亚历克斯的教训:每次命名时都要非常小心。
回答by aib
An excerpt from The C++ Programming Languageby Bjarne Stroustrup:
摘自Bjarne Stroustrup 的The C++ Programming Language:
"One could argue that valarrayshould have been called vectorbecause it is a traditional mathematical vector and that vectorshould have been called array. However, this is not the way the terminology evolved."
“有人可能会争辩说valarray应该被称为向量,因为它是一个传统的数学向量,而该向量应该被称为array。但是,这不是术语演变的方式。”
回答by vartec
The name comes from the linear algebra, where vector is matrix with only one column or only one row.
这个名字来自线性代数,其中向量是只有一列或只有一行的矩阵。
回答by Johannes Schaub - litb
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:
只是说为什么它可能不被称为array
:因为std::vector
具有动态大小。数组在概念上是固定长度的。顺便说一下,下一个 C++ 标准有一个std::array
模板,它的大小是固定的,应该优先于普通数组:
std::array<int, 4> f = { 1, 2, 3, 4 };
回答by Fernando Pelliccioni
To complement the excellent response from @MarkRuzon:
为了补充@MarkRuzon 的出色回应:
Alex said that to give a name to what is now called std::vector he observed the name that Schemeand Common Lisp??had given to similar data structures.
Alex 说,为了给现在称为 std::vector 的东西命名,他观察了Scheme和Common Lisp为类似数据结构赋予的名称。
Later he admits he was wrong because C++vector has nothing to do with the vectors in mathematics.
后来他承认他错了,因为C++向量与数学中的向量无关。
He also says that he introduced an error of a community of 50 people to a community of 5 million people, so the error is likely to remain forever.
他还说,他将 50 人社区的错误引入了 500 万人的社区,因此该错误很可能会永远存在。
回答by 4pie0
It is just the name. C++ vectorcould very well (or maybe even more accurate) be called dynamic arrayor resizable arraybut this name was simply chosen. This vectoris not the same as vector from methematics because in mathematics vectors are members of any set Vsuch that there are two important operationsdefined on this set: +(addition of vectors) and x(multiplication of a vector by a scalar from field F) and these operations satisfy 8 axioms:
这只是名字。C++向量可以很好地(或者甚至更准确)称为动态数组或可调整大小的数组,但只是简单地选择了这个名称。这个向量与数学中的向量不同,因为在数学中,向量是任何集合V 的成员,因此在这个集合上定义了两个重要的运算:+(向量相加)和x(向量乘以来自域的标量)F) 并且这些操作满足 8 个公理:
Associativity of addition
加法的结合性
u + (v + w) = (u + v) + w
u + (v + w) = (u + v) + w
Commutativity of addition
加法的交换性
u + v = v + u
u + v = v + u
Identity element of addition
加法的身份元素
There exists an element 0 ∈ V, called the zero vector, such that v + 0 = v for all v ∈ V.
存在一个元素 0 ∈ V,称为零向量,使得 v + 0 = v 对于所有 v ∈ V。
Inverse elements of addition
加法的逆元素
For every v ∈ V, there exists an element ?v ∈ V, called the additive inverseof v, such that v + (?v) = 0
对于每个 v ∈ V,存在一个元素 ?v ∈ V,称为v的加法逆,使得 v + (?v) = 0
Compatibility of scalar multiplication with field multiplication
标量乘法与域乘法的兼容性
a(bv) = (ab)v
a(bv) = (ab)v
Identity element of scalar multiplication
标量乘法的单位元
1v = v, where 1denotes the multiplicative identityin F.
1个V = V,其中1表示乘法单位中˚F。
Distributivity of scalar multiplication with respect to vector addition??
标量乘法相对于向量加法的分布性??
a(u + v) = au + av
a(u + v) = au + av
Distributivity of scalar multiplication with respect to field addition
标量乘法相对于域加法的分布
(a + b)v = av + bv
(a + b)v = av + bv
C++ std::vector
supports all of them (not directly, but via C++ features), so it can somehow be called a vector, but it is just colloquialism and for example Vallaray
pointed out by Bjarne Stroustrup in "C++ Programming Language"supports some of them directly.
C++std::vector
支持所有这些(不是直接,而是通过 C++ 特性),所以它可以以某种方式称为向量,但这只是口语化,例如Vallaray
Bjarne Stroustrup 在“C++ 编程语言”中指出的直接支持其中的一些。
回答by FrankHB
Long time ago, in the B languagethere are vector types. Then the C language called them "arrays". Then the C with Classes and the C++ language just derived it ...
很久以前,在B 语言中有向量类型。然后 C 语言将它们称为“数组”。然后带有类的 C 和 C++ 语言只是派生它......
This is certainly not the whole story. As mentioned above, Stepanov made the actual decision. But if "vector" was still used in C, the result maybe looks quite different.
这当然不是故事的全部。如上所述,斯捷潘诺夫做出了实际的决定。但是如果在 C 中仍然使用“vector”,结果可能看起来完全不同。
PS. I wonder why C renames "array". What was the exact reason?
附注。我想知道为什么 C 重命名“数组”。确切的原因是什么?
PS2. IMO for a language as C++, an array is better meaning "a type hold elements to be reasonably accessed via operator[]" (i.e. not 42[some_array_object]), e.g. an instantiation of std::map as an "associative array".
PS2。对于 C++ 语言的 IMO,数组更好的意思是“一种类型保存可以通过 operator[] 合理访问的元素”(即不是 42[some_array_object]),例如将 std::map 实例化为“关联数组”。
回答by James Matta
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.
此外,如果您让它存储整数或浮点数,它确实是存储 N 维向量的绝佳类型。毕竟向量是一个按特定顺序保存的数字列表。
回答by unwind
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.
向量只是一个值序列,所有的类型都相同。这与数学中的使用非常一致。我猜向量应该支持一些常见操作(例如加法和标量缩放)的数学思想没有被继承,重要的方面主要是结构。