gpt4 book ai didi

c++ - 为什么std::vector和std::valarray初始化构造函数不同?

转载 作者:行者123 更新时间:2023-12-01 09:01:58 27 4
gpt4 key购买 nike

我刚刚被以下事情打伤了。
如果要使用常量std::vector初始化n元素的X,请执行以下操作:

std::vector<double> v(n, X);
但是,如果我需要使用常量 std::valarray初始化 n元素的 X,则需要交换大小并初始化常量:
std::valarray<double> va(X, n);
在我看来,这就像一个任意的“陷阱”。
在对 std::vectorstd::valarray进行标准化时,确定填充构造函数的参数顺序时,标准委员会是否提供技术原因或某些设计依据?

最佳答案

因为它们不是来自同一个地方:vector来自STL库,而valarray不是(我无法确定它的来源,但似乎与Fortran有很强的联系)。
引用比耶恩本人的话:

Most of the time, work on each of these components progressed inisolation from work on the others. There was no overall design ordesign philosophy.
[...]
Basically, the committee failed to contain “design by committee” sowhereas the STL reflects a clear philosophy and coherent style, mostof the other components suffered. Each represents its own style andphilosophy, and some (such as string) manage simultaneously to present several.


(摘自“在现实世界中为现实世界发展一种语言:C++ 1991-2006”。)
因此,我想说的是,传统的C++语言是这样的:“事物就是它们的样子,为实现标准化而对其进行更改会破坏很多事情,所以让我们保持足够的孤独”。

关于c++ - 为什么std::vector和std::valarray初始化构造函数不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62793616/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com