为什么在参数中使用initializer_list而不是vector? [英] Why use initializer_list instead of vector in parameters?

查看:56
本文介绍了为什么在参数中使用initializer_list而不是vector?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于未知数量的参数, initializer_list 的实际好处和目的是什么?为什么不只使用 vector 并完成它呢?

What is the actual benefit and purpose of initializer_list, for unknown number of parameters? Why not just use vector and be done with it?

实际上,这听起来像是带有另一个名称的 vector .为什么要打扰?

In fact, it sounds like just a vector with another name. Why bother?

我看到的 initializer_list 的唯一好处"是它具有 const 元素,但这似乎不足以发明这种全新类型.(毕竟,您只能使用 const向量.)

The only "benefit" I see of initializer_list is that it has const elements, but that doesn't seem to be a reason enough to invent this whole new type. (You can just use a const vector after all.)

那我在想什么?

推荐答案

这是程序员和编译器之间的一种契约.程序员说 {1,2,3,4} ,然后编译器从中创建一个 initializer_list< int> 类型的对象,其中包含相同的元素序列.它.该约定是语言规范对编译器实现施加的要求.

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

这意味着,不是由程序员手动创建此类对象,而是由编译器创建该对象,并将该对象传递给以 initializer_list< int> 为参数的函数.

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<int> as argument.

std :: vector 实现利用了该协定,因此它定义了一个构造函数,该构造函数将 initializer_list< T> 作为参数,以便可以初始化自身带有初始化列表中的元素.

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

现在假设一会儿 std :: vector 没有任何采用 std :: initializer_list< T> 的构造函数作为参数,那么您将得到:

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

 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_list std :: vector 可以根据表达式 {1,2,3,4} .

As per the assumption, since std::vector doesn't have constructor that takes std::initializer_list<T> 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}.

现在,您将了解,只要需要以 {value1,value2,....,valueN}形式的表达式,就可以使用 std :: initializer_list .这就是为什么标准库中的其他容器也定义了以 std :: initializer_list 作为参数的构造函数的原因.这样,任何容器都不会依赖任何其他容器来构建 {value1,value2,....,valueN} 形式的表达式.

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

希望有帮助.

这篇关于为什么在参数中使用initializer_list而不是vector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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