不能在向量向量上 emplace_back() 一个带支撑的初始值设定项 [英] Cannot emplace_back() a braced initializer on a vector of vectors
问题描述
这与我之前提出的关于在成对向量上使用 emplace_back
的问题有些相关.emplace_back() vs push_back 插入一对时std::vector
This is somewhat related to a previous question I asked regarding using emplace_back
on a vector of pairs. emplace_back() vs push_back when inserting a pair into std::vector
现在我的问题是关于在向量向量上使用 emplace_back
.
Now my question pertains to using emplace_back
on a vector of vectors.
这是我用评论质疑的代码
Here is the code that I am questioning about with comments
std::vector<std::vector<int>> matrix;
matrix.emplace_back({1,2,3}); //doesn't compile
matrix.emplace_back(1,2,3); //doesn't compile
matrix.push_back({1,2,3}); //works and does what is expected (insert a vector made of {1,2,3} into matrix);
matrix.emplace_back(std::vector<int>{1,2,3}); //works but
//defeats the purpose of using emplace_back since this makes a copy
//and is thus equivalent to push_back in this case?
matrix.emplace_back(3,2) //this compiles,
//but it seems to insert a vector of size 3 made of 2s into the matrix.
//not actually sure why it does this
所以,由此看来,matrix.emplace_back(std::vector
似乎是使用 std:: 的唯一正确方法vector<T>::emplace_back
在向量的向量上,但这似乎比 push_back
没有任何优势.我对这个问题的理解是否正确?
So, from this, matrix.emplace_back(std::vector<int>{1,2,3});
seems to be the only correct way to use std::vector<T>::emplace_back
on a vector of vectors, but this seems to offer no advantages over push_back
. Is my understanding on this matter correct?
另外,有人能解释一下为什么 matrix.emplace_back(3,2)
将一个由 2s 组成的大小为 3 的向量插入矩阵吗?
Also, could someone explain why matrix.emplace_back(3,2)
is inserting a vector of size 3 made of 2s into the matrix?
推荐答案
{1, 2, 3}
在这种情况下不能推导出initializer_list
(这是您想要使用的 vector
构造函数所期望的.)所以您需要帮助它:
The {1, 2, 3}
cannot be deduced to initializer_list<int>
in this case (which is what the vector<int>
constructor you want to use expects.) So you need to help it along a bit:
matrix.emplace_back(initializer_list<int>{1, 2, 3});
当使用 push_back()
时,这不是必需的.我不知道确切的细节,但 emplace_back()
是一个函数模板,而 push_back()
不是.模板的推导规则是不同的(更严格.)并且花括号初始化器没有类型.正因为如此,它有自己的关于类型推导如何工作的特殊规则.
This is not required when using push_back()
. I don't know the exact details, but emplace_back()
is a function template while push_back()
is not. Deduction rules for templates are different (way more strict.) And a braced initializer has no type. Because of that, it comes with its own special rules on how type deduction works.
至于高效,这个:
matrix.emplace_back(vector<int>{1, 2, 3});
构造两个向量.matrix
中的空向量,以及传递的临时值.临时文件被移动到空向量中.所以真的没那么糟糕.
constructs two vectors. An empty vector in matrix
, and the passed temporary. The temporary is moved into the empty vector. So it's not that bad really.
然而,这:
matrix.emplace_back(initializer_list<int>{1, 2, 3});
只构造一个向量,使用接受初始化列表的构造函数.请注意,这里没有创建额外"的 initializer_list
对象.在使用花括号初始化创建任何向量时,无论如何都会创建这样的对象:
Only constructs one vector, using the constructor that accepts an initializer_list. Note that there's no "extra" initializer_list
object being created here. Such an object is going to be created anyway when creating any vector using braced initialization:
vector<int> vec{1, 2, 3};
这也创建了一个 initializer_list
对象,因为这是向量构造函数所需要的.
This also creates an initializer_list
object, because that's what the vector constructor takes.
至于为什么 emplace_back(2,3)
起作用,那是因为有一个接受大小和值的向量构造函数.
As for why emplace_back(2,3)
works, that's because there's a vector constructor that takes a size and a value.
这篇关于不能在向量向量上 emplace_back() 一个带支撑的初始值设定项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!