是否有将范围移动到向量的标准方法? [英] Is there a standard way of moving a range into a vector?
问题描述
考虑以下程序,该程序将一系列元素插入向量中:
Consider the following program which inserts a range of elements into a vector:
vector<string> v1;
vector<string> v2;
v1.push_back("one");
v1.push_back("two");
v1.push_back("three");
v2.push_back("four");
v2.push_back("five");
v2.push_back("six");
v1.insert(v1.end(), v2.begin(), v2.end());
这可以有效地复制范围,在目标矢量中为整个范围分配足够的空间,从而最大需要一个大小调整。现在考虑以下试图将范围移动到向量中的程序:
This efficiently copies the range, allocating enough space in the target vector for the entire range so that a maximum of one resize will be required. Now consider the following program which attempts to move a range into a vector:
vector<string> v1;
vector<string> v2;
v1.push_back("one");
v1.push_back("two");
v1.push_back("three");
v2.push_back("four");
v2.push_back("five");
v2.push_back("six");
for_each ( v2.begin(), v2.end(), [&v1]( string & s )
{
v1.emplace_back(std::move(s));
});
此操作执行成功,但不享受insert()在预分配方面的好处目标向量中有空格,因此在操作过程中可以对向量进行多次调整。
This performs a successful move but doesn't enjoy the benefits that insert() has with regard to preallocating space in the target vector, so the vector could be resized several times during the operation.
所以我的问题是,是否有一个插入等价物可以将范围移动到向量中?
So my question is, is there an insert equivalent which can move a range into a vector?
推荐答案
您将 move_iterator
与 insert
结合使用:
v1.insert(v1.end(), make_move_iterator(v2.begin()), make_move_iterator(v2.end()));
24.5.3中的示例几乎完全是这样。
The example in 24.5.3 is almost exactly this.
如果(a) vector :: insert
使用迭代器标记分派来检测随机访问迭代器并预先计算大小,您将获得所需的优化(您在示例中假设已完成复制),并且(b) move_iterator
保留了它包装的迭代器的迭代器类别(标准要求)
You'll get the optimization you want if (a) vector::insert
uses iterator-tag dispatch to detect the random-access iterator and precalculate the size (which you've assumed it does in your example that copies), and (b) move_iterator
preserves the iterator category of the iterator it wraps (which is required by the standard).
在一个模糊的地方:我很确定 vector :: insert
可以从源代码放置(由于来源与目的地的类型相同,因此此处无关紧要,因此,位置与复制/移动相同,但与其他示例相同。我尚未找到执行此操作所需的语句,我只是从以下事实推断出来的:迭代器对 i,j
的要求已传递给 insert
是 T
是 * i
。
On an obscure point: I'm pretty sure that vector::insert
can emplace from the source (which is irrelevant here, since the source is the same type as the destination, so an emplace is the same as a copy/move, but would be relevant to otherwise-identical examples). I haven't yet found a statement that it's required to do so, I've just inferred it from the fact that the requirement on the iterator pair i,j
passed to insert
is that T
be EmplaceConstructible
from *i
.
这篇关于是否有将范围移动到向量的标准方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!