为什么std :: vector和std :: valarray初始化构造函数不同? [英] Why are std::vector and std::valarray initializing constructors different?

查看:81
本文介绍了为什么std :: vector和std :: valarray初始化构造函数不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚被下列事情所烧死.

如果要使用常量 X 初始化 n 个元素的 std :: vector ,我可以这样做:

  std :: vector< double>v(n,X); 

但是,如果我需要使用常量 X 初始化 n 个元素的 std :: valarray ,我需要交换大小并进行初始化常数:

  std :: valarray< double>va(X,n); 

在我看来,这就像一个任意的陷阱".

在确定 std :: vector std :: valarray 标准化了吗?

解决方案

因为它们来自不同的地方: vector 来自STL库,而 valarray 不会(我无法确定它的来源,但似乎与Fortran有很强的联系).

引用Bjarne自己的话:

大多数时候,这些组件的工作都在与他人隔离.没有整体设计或设计理念.
[...]
基本上,委员会没有包含按委员会设计"的内容,因此而STL则体现了清晰的哲学和连贯的风格,其他组成部分遭受了损失.每个代表自己的风格,哲学和一些(例如 string )同时管理以呈现多个.

(摘自在现实世界中以及为现实世界发展一种语言:C ++ 1991-2006".)

因此,我想说的是,传统的C ++语言是这样的,事物就是它们的样子,为实现标准化而对其进行更改将破坏很多事情,所以让我们保持足够的孤独".>

I have just been burned by the following.

If I want to initialize a std::vector of n elements with a constant X I do this:

std::vector<double> v(n, X);

But if I need to initialize a std::valarray of n elements with a constant X I need to swap the size and initializing constant:

std::valarray<double> va(X, n);

This seems to me like an arbitrary 'gotcha'.

Is there a technical reason or some design rationale provided by the standards committee when deciding about the order of the fill constructor's parameters when std::vector and std::valarray were standardized?

解决方案

Because they don't come from the same place: vector comes from the STL library, and valarray doesn't (I haven't been able to find out where it comes from, but there seem to be strong connections to Fortran).

Quoting Bjarne himself:

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 as string) manage simultaneously to present several.

(From "Evolving a language in and for the real world: C++ 1991-2006".)

So I would say that the rationale is the traditional C++ one, "things are the way they are, and changing them for the sake of Standardization would break a lot of things, so let's leave well enough alone".

这篇关于为什么std :: vector和std :: valarray初始化构造函数不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆