c++ 为什么在参数中使用initializer_list而不是vector?

c++ 为什么在参数中使用initializer_list而不是vector?

原文 http://stackoverflow.com/questions/14414832/why-use-initializer-list-instead-of-vector-in-parameters
原文 http://www.voidcn.com/article/p-prjjfdcz-btc.html

initializer_list的实际利益和目的是什么?为什么不只是使用矢量并完成它?
其实呢,听起来就像是另一个名字的矢量。何必?

我看到的initializer_list唯一的“好处”是它有const元素,但似乎并不足以发明这种全新的类型。 (你可以使用一个常量向量。)

那么,我在做什么?

这是程序员和编译器之间的一种契约。程序员说{1,2,3,4},编译器创建一个类型为initializer_list< int>的对象。其中包含相同的元素序列。该合同是语言规范对编译器实现所强加的要求。
这意味着,不是手动创建这样的对象的程序员,而是创建对象的编译器,并将该对象传递给使用initializer_list< int>的函数。作为论证。

std :: vector实现利用了这个契约,因此它定义了一个构造函数,它接受initializer_list< T>作为参数,以便它可以使用initializer-list中的元素来初始化自身。

现在假设一段时间,std :: vector没有任何构造函数,它接受std :: initializer_list< T>作为论据,那么你会得到这样的:

void f(std::initializer_list<int> const &items);
void g(std::vector<int> const &items);

 f({1,2,3,4}); //okay
 g({1,2,3,4}); //error (as per the assumption)

根据这个假设,因为std :: vector不具有使用std :: initializer_list< T>的构造函数作为参数,这意味着你不能将{1,2,3,4}作为参数传递给g(),如上所示,因为编译器无法从表达式{1,2,3,4}中直接创建一个std :: vector的实例,这是因为程序员和编译器之间没有这样的契约,而是被语言所强加。它是通过std :: initializer_liststd :: vector能够创建自己的表达式{1,2,3,4}

现在,您将明白std :: initializer_list可以在需要{value1,value2,…,valueN}形式的表达式的地方使用。这就是为什么标准库中的其他容器也定义了以std :: initializer_list为参数的构造函数。以这种方式,任何容器都不会依赖任何其他容器来构造,形式为{value1,value2,…,valueN}

希望有帮助。

It is a sort of contract between the programmer and the compiler. The programmer says {1,2,3,4}, and the compiler creates an object of type initializer_list out of it, containing the same sequence of elements in it. This contract is a requirement imposed by the language specification on the compiler implementation.

That means, it is not the programmer who creates manually such an
object but it is the compiler which creates the object, and pass that
object to function which takes initializer_list as argument.

The std::vector implementation takes advantage of this contract, and
therefore it defines a constructor which takes initializer_list as
argument, so that it could initialize itself with the elements in the
initializer-list.

Now suppose for a while that the std::vector doesn’t have any
constructor that takes std::initializer_list as argument, then you
would get this:

void f(std::initializer_list const &items); void
g(std::vector const &items);

f({1,2,3,4}); //okay g({1,2,3,4}); //error (as per the assumption)
As per the assumption, since std::vector doesn’t have constructor that
takes std::initializer_list as argument, which implies you cannot
pass {1,2,3,4} as argument to g() as shown above, because the compiler
cannot create an instance of std::vector out of the expression
{1,2,3,4} directly. It is because no such contract is ever made
between programmer and the compiler, and imposed by the language. It
is through std::initializer_list, the std::vector is able to create
itself out of expression {1,2,3,4}.

Now you will understand that std::initializer_list can be used
wherever you need an expression of the form of {value1, value2, …,
valueN}. It is why other containers from the Standard library also
define constructor that takes std::initializer_list as argument. In
this way, no container depends on any other container for construction
from expressions of the form of {value1, value2, …, valueN}.

Hope that helps.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值