为什么 C++ 向量称为向量?

发布于 2024-07-14 06:24:47 字数 1705 浏览 6 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

多彩岁月 2024-07-21 06:24:48

此外,如果你让它存储整数或浮点数,它确实是存储 N 维向量的绝佳类型。 毕竟向量是按特定顺序保存的数字列表。

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.

李白 2024-07-21 06:24:48

将 C++ 向量视为动态数组,可以通过插入或删除元素来更改其大小。 它们与向量的数学定义无关。

数学中的向量

考虑一个名为 Anxm 矩阵,其中 n 对应于行数, >m 对应于列数。 在数学上下文中,一旦引入这样的矩阵,之后就无法执行 A 范围之外的任何操作,也无法扩展 A的大小。
这意味着您不能引用 [n + 1] 和/或 [m + 1] 的索引。

现在,A 向量也派生这些属性,而它们的尺寸将始终为 1xm(在 [i] 中选择的任何 [i] 行) >A)
nx1(在 A 中选择的任何 [j] 列)。
向量也不能指定为 2xn,因为向量集合不能解释为 一个 向量,而一个向量 - 令其为 [i]< /code> 尺寸为 1xmA 列向量 - 可以解释为矩阵。

重要的一点是,一旦以数学形式引入向量,就无法更改向量的维度。

C++ 中的向量

在 C++ 中,向量就像数学中的向量一样,但与数学不同的是它们的大小可以改变。 大小作为一个术语适用于此,因为它意味着一个特定向量包含的元素数量。

当您有一个由向量​​组成的向量时,您可以使用 C++ 向量中的术语维度:std::vector>>> ragged_array。
在这个例子中,我称该向量为“ragged”,因为
它演示了如何独立改变该向量的每个向量的大小。 它不仅违反了数学中引入特定向量后维数不能更改的规则,而且还演示了它不能用作矩阵。

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<std::vector<T>>> 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.

困倦 2024-07-21 06:24:48

我猜它来自术语行向量。 此外,计算机科学家喜欢为事物想出新名称......

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

流年里的时光 2024-07-21 06:24:48

有很多好的否定答案,即向量不是一个好名字。 我想添加更多信息,为什么它被称为向量,以及向量这个词在计算行业中的使用方式。

Vector 字面意思是“载体”。 然而,主要意义源自数学用法,意思是:

  1. 有向量(19世纪早期)
  2. 有序数字集(19世纪后期)

第二个意义扩展到计算,我们有“内存中连续位置的序列”(< em>简明牛津英语词典)。 它与数组具有相似的含义,并且我们有这样的术语:

  • 向量处理器
  • 向量指令
  • 向量化
  • 初始化向量
  • 中断向量

它们是既定用法,因此C++使用向量不是错误的。 然而,正如 Stepanov 先生所说(在从数学到通用编程中,当他谈到命名原则时):

如果存在冲突的用法,则更成熟的用法获胜。

他的遗憾实际上是向量的这种使用与(更成熟的)数学用法相冲突,并且最好一开始就避免它。

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.

荒路情人 2024-07-21 06:24:48

为了补充 Mark Ruzon 的答案,以下是 Alex Stepanov 在他 2015 年的书《从数学到通用编程》(与 Daniel Rose 合着)中的话:

STL 中的名称​​向量取自早期的编程语言Scheme 和Common Lisp。 不幸的是,这与该术语在数学中更古老的含义不一致……这种数据结构应该被称为数组。 遗憾的是,如果你犯了一个错误……结果可能会持续很长时间。

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.

美胚控场 2024-07-21 06:24:48

不知道真正的原因,但 C++ 将其称为向量而不是数组,减少了 C 和 C++ 结构之间的混淆,尽管它们履行相同的角色。

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.

黎夕旧梦 2024-07-21 06:24:48

它来自由向量构建的矩阵结构

it comes from the structure of matrix which build from vectors

染墨丶若流云 2024-07-21 06:24:47

它被称为向量是因为标准模板库的设计者 Alex Stepanov 正在寻找一个名称来将其与内置数组区分开来。 他现在承认自己犯了一个错误,因为数学已经使用术语“向量”来表示固定长度的数字序列。 C++11 通过引入一个行为类似于数学向量的“数组”类来加剧这个错误。

亚历克斯的教训:每次命名某物时都要非常小心。

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.

独留℉清风醉 2024-07-21 06:24:47

向量的数学定义是集合 Sn 的成员,它是特定集合 ( S)。 这就是 C++ vector 存储的内容。

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.

初见终念 2024-07-21 06:24:47

Bjarne Stroustrup 摘自《C++ 编程语言》:

“有人可能会说valarray
应该被称为向量
因为它是传统的
数学向量和向量
应该被称为数组
然而,这并不是方法
术语不断演变。”

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

眼泪也成诗 2024-07-21 06:24:47

这个名字来源于线性代数,其中向量是只有一列或只有一行的矩阵。

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

撑一把青伞 2024-07-21 06:24:47

为了补充 @MarkRuzon 的出色回应:

Alex 说,为了给现在称为 std::vector 的东西命名,他观察到了 SchemeCommon Lisp 给出了类似的数据结构。

后来他承认自己错了,因为 C++ 向量与数学中的向量毫无关系。

他还表示,他将一个 50 人的社区的错误引入了一个 500 万人的社区,因此该错误很可能永远存在。

To complement the excellent response from @MarkRuzon:

Alex said that to give a name to what is now called std::vector he observed the name that Scheme and Common Lisp ​​had given to similar data structures.

Later he admits he was wrong because C++ vector has nothing to do with the vectors in mathematics.

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.

弥繁 2024-07-21 06:24:47

只是想说为什么它可能不被称为array:因为std::vector具有动态大小。 从概念上讲,数组的长度是固定的。 顺便说一句,下一个 C++ 标准有一个 std::array 模板,它的大小是固定的,应该优先于普通数组:

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

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 };
琉璃梦幻 2024-07-21 06:24:47

这只是名字。 C++向量很可能(或者可能更准确)被称为动态数组可调整大小的数组,但这个名称只是选择的< /强>。 这个向量与数学中的向量不同,因为在数学中,向量是任何集合V的成员,因此定义了两个重要的运算该集合:+(向量相加)和x(向量与域F中的标量相乘)以及这些运算满足8个公理


加法结合律

u + (v + w) = (u + v) + w

加法交换律

u + v = v + u

加法的恒等元

存在一个元素 0 ∈ V,称为零向量,使得 v + 0 = v 对于所有 v ∈ <强>V。

加法逆元

对于每个 v ∈ V,存在一个元素 -v ∈ V,称为 v 的加法逆元,使得 v + (−v) = 0

标量乘法与域乘法的兼容性

a(bv) = (ab)v

标量乘法的恒等元

< strong>1 v = v,其中1表示F中的乘法恒等式

标量乘法相对于向量加法的分布性

a(u + v) = au + av

标量乘法相对于场加法的分布性

(a + b)v = av + bv


C++ std::vector 支持所有这些(不是直接支持,而是通过 C++ 功能),因此它可以以某种方式被称为向量,但这只是口语化,例如 Vallaray< Bjarne Stroustrup 在《C++ 编程语言》中指出的 /code> 直接支持其中的一些。

It is just the name. C++ vector could very well (or maybe even more accurate) be called dynamic array or resizable array but this name was simply chosen. This vector is not the same as vector from methematics because in mathematics vectors are members of any set V such that there are two important operations defined on this set: + (addition of vectors) and x (multiplication of a vector by a scalar from field F) and these operations satisfy 8 axioms:


Associativity of addition

u + (v + w) = (u + v) + w

Commutativity of addition

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.

Inverse elements of addition

For every v ∈ V, there exists an element −v ∈ V, called the additive inverse of v, such that v + (−v) = 0

Compatibility of scalar multiplication with field multiplication

a(bv) = (ab)v

Identity element of scalar multiplication

1 v = v, where 1 denotes the multiplicative identity in F.

Distributivity of scalar multiplication with respect to vector addition  

a(u + v) = au + av

Distributivity of scalar multiplication with respect to field addition

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

姜生凉生 2024-07-21 06:24:47

很久以前,B 语言中有矢量类型。 后来C语言称它们为“数组”。 然后 C with Classes 和 C++ 语言就衍生出了它……

这当然不是故事的全部。 正如提到的,斯捷潘诺夫做出了实际的决定。 但如果在 C 中仍然使用“向量”,结果可能看起来完全不同。

附言。 我想知道为什么 C 重命名“array”。 确切的原因是什么?

PS2。 IMO 对于 C++ 语言,数组更好的意思是“一个类型保存可以通过 operator[] 合理访问的元素”(即不是 42[some_array_object]),例如std::map 的实例化为“关联数组” 。

Long time ago, in the B language there are vector types. Then the C language called them "arrays". Then the C with Classes and the C++ language just derived it ...

This is certainly not the whole story. As mentioned, Stepanov made the actual decision. But if "vector" was still used in C, the result maybe looks quite different.

PS. I wonder why C renames "array". What was the exact reason?

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

2024-07-21 06:24:47

向量只是一系列相同类型的值。 这与数学中的用法非常一致。 我想向量应该支持一些常见操作(例如加法和标量缩放)的数学思想没有被继承,重要的方面主要是结构。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文