我刚刚被以下事情打伤了。
如果要使用常量std::vector
初始化n
元素的X
,请执行以下操作:
std::vector<double> v(n, X);
但是,如果我需要使用常量std::valarray
初始化n
元素的X
,则需要交换大小并初始化常量:std::valarray<double> va(X, n);
在我看来,这就像一个任意的“陷阱”。在对
std::vector
和std::valarray
进行标准化时,确定填充构造函数的参数顺序时,标准委员会是否提供技术原因或某些设计依据?
最佳答案
因为它们不是来自同一个地方:vector
来自STL库,而valarray
不是(我无法确定它的来源,但似乎与Fortran有很强的联系)。
引用比耶恩本人的话:
Most of the time, work on each of these components progressed in isolation from work on the others. There was no overall design or design philosophy.
[...]
Basically, the committee failed to contain “design by committee” so whereas the STL reflects a clear philosophy and coherent style, most of the other components suffered. Each represents its own style and philosophy, and some (such asstring
) manage simultaneously to present several.
(摘自“在现实世界中为现实世界发展一种语言:C++ 1991-2006”。)
因此,我想说的是,传统的C++语言是这样的:“事物就是它们的样子,为实现标准化而对其进行更改会破坏很多事情,所以让我们保持足够的孤独”。
关于c++ - 为什么std::vector和std::valarray初始化构造函数不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62793616/