English 中文(简体)
是否有一种标准的方式将一个范围移动到矢量中?
原标题:Is there a standard way of moving 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");

v1.insert(v1.end(), v2.begin(), v2.end());

这样可以有效地复制范围, 在整个范围内在目标矢量中分配足够的空间, 从而需要最大一次调整。 现在考虑以下程序, 试图将一个范围移动到矢量 :

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));
});

这执行一个成功的移动, 但并享受不到插入( ) 在目标矢量中预分配空间方面的好处, 使矢量在操作期间可以调整几次大小 。

所以,我的问题是,有没有一个插入等同的插件, 可以将一个范围移动到矢量中?

最佳回答

您使用 move_ terator 插入 :

v1.insert(v1.end(), make_move_iterator(v2.begin()), make_move_iterator(v2.end()));

24.5.3中的例子几乎就是这个。

如果 (a) < code> victor:: Indige: 使用传动器- tag 发送来检测随机访问传动器,并预估大小(在复制的示例中,你曾经假设会这样做),以及 (b) < code>move_ terator 保存着它包装的迭代器的迭代器类别(这是标准要求的)。

关于一个模糊点:我很确定 < code> victor:: Indig 可以从源中插入(此处无关紧要,因为源与目的类型相同,所以一个源与目的类型相同,因此一个源与目的类型相同,因此一个源与副本/移动相同,但与其它相同的例子相关)。我还没有找到要求它这样做的语句,我刚刚从以下事实中推断出,即对代号对 i,j 传递给 Indig 的要求是, T 应该是 Emplace Construcable

问题回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签