在 C++17 中 std::vector::emplace_back 的 return 值的用例是什么?
What is the usecase of return value for std::vector::emplace_back in C++17?
我在 cppreference.com 中读到,新的(自 C++17 起)std::vector::emplace_back
具有对插入元素的 return 引用值。
Return value
- (none) (until C++17)
- A reference to the inserted element. (since C++17)
我在想,在向向量插入元素时,为什么我们需要引用它?这有什么用,或者这个新 return 的用例是什么?
这是我写的示例代码,用于查看该功能。
#include <vector>
int main()
{
std::vector<int> myVec;
for(int i = 0; i < 3; ++i)
{
int& newElement = myVec.emplace_back(i);
^^^^^^^ => why standard should expose the element after inserting.
}
}
std::cout << vec.emplace_back(7);
这只是方便,因此您可以在一个表达式中做不止一件事。
vec.emplace_back().reg();
它所做的任何事情都可以被
复制
(void(vec.emplace_back()), vec.back()).reg();
在 C++17 之前的版本中(此处为空是面向未来的偏执狂)
创建一个对象并立即使用它是一种常见的模式; return 值使它稍微容易一些。
更改由 P0084 完成。作者给出的动力是
I often find myself wanting to create an element of a container using emplace_front
or emplace_back
, and then access that element, either to modify it further or simply to use it. So I find myself writing code like this:
my_container.emplace_back(...);
my_container.back().do_something(...);
Or perhaps:
my_container.emplace_back(...);
do_something_else(my_container.back());
Quite a common specific case is where I need to construct an object before I have all the
information necessary to put it into its final state, such as when I’m reading it from a file:
my_container.emplace_back(); // Default construct.
my_container.back().read(file_stream); // Read the object.
This happens often enough that I tend to write little templates that call some version of emplace
and return back, which seems rather unnecessary to me. I believe the emplace_front
and emplace_back
functions should return a non-const reference to the newly created element, in keeping with the current Standard Library trend of returning useful information when practical. It was an oversight (on my part) in the original emplace
proposal that they do not.
崩溃警报!
使用(返回的)引用要非常小心,尤其是在添加另一个元素之后。然后 vector 倾向于重新分配内存,并且第一个返回的引用不再有效,导致内存异常!
示例:
std::vector <int> intVector;
int& a0 = intVector.emplace_back(0);
int& a1 = intVector.emplace_back(1); // a0& invalid
int& a2 = intVector.emplace_back(2); // a0& and a1& invalid
如果你在最后查看矢量本身,一切似乎都很好 {0,1,2}。如果你查看 a0 和 a1,那只是垃圾。
原因:de vector 已被重新分配,使引用指向无处。
顺便说一句:出于同样的原因,使用 back() 函数也会给你带来麻烦。
我在 cppreference.com 中读到,新的(自 C++17 起)std::vector::emplace_back
具有对插入元素的 return 引用值。
Return value
- (none) (until C++17)
- A reference to the inserted element. (since C++17)
我在想,在向向量插入元素时,为什么我们需要引用它?这有什么用,或者这个新 return 的用例是什么?
这是我写的示例代码,用于查看该功能。
#include <vector>
int main()
{
std::vector<int> myVec;
for(int i = 0; i < 3; ++i)
{
int& newElement = myVec.emplace_back(i);
^^^^^^^ => why standard should expose the element after inserting.
}
}
std::cout << vec.emplace_back(7);
这只是方便,因此您可以在一个表达式中做不止一件事。
vec.emplace_back().reg();
它所做的任何事情都可以被
复制(void(vec.emplace_back()), vec.back()).reg();
在 C++17 之前的版本中(此处为空是面向未来的偏执狂)
创建一个对象并立即使用它是一种常见的模式; return 值使它稍微容易一些。
更改由 P0084 完成。作者给出的动力是
I often find myself wanting to create an element of a container using
emplace_front
oremplace_back
, and then access that element, either to modify it further or simply to use it. So I find myself writing code like this:my_container.emplace_back(...); my_container.back().do_something(...);
Or perhaps:
my_container.emplace_back(...); do_something_else(my_container.back());
Quite a common specific case is where I need to construct an object before I have all the information necessary to put it into its final state, such as when I’m reading it from a file:
my_container.emplace_back(); // Default construct. my_container.back().read(file_stream); // Read the object.
This happens often enough that I tend to write little templates that call some version of
emplace
and return back, which seems rather unnecessary to me. I believe theemplace_front
andemplace_back
functions should return a non-const reference to the newly created element, in keeping with the current Standard Library trend of returning useful information when practical. It was an oversight (on my part) in the originalemplace
proposal that they do not.
崩溃警报! 使用(返回的)引用要非常小心,尤其是在添加另一个元素之后。然后 vector 倾向于重新分配内存,并且第一个返回的引用不再有效,导致内存异常! 示例:
std::vector <int> intVector;
int& a0 = intVector.emplace_back(0);
int& a1 = intVector.emplace_back(1); // a0& invalid
int& a2 = intVector.emplace_back(2); // a0& and a1& invalid
如果你在最后查看矢量本身,一切似乎都很好 {0,1,2}。如果你查看 a0 和 a1,那只是垃圾。 原因:de vector 已被重新分配,使引用指向无处。 顺便说一句:出于同样的原因,使用 back() 函数也会给你带来麻烦。