我使用C++ 11,我喜欢插入一个载体向另一个载体的特定位置,这里是一个简化的代码:C++ 11插入一个载体导入特定位置
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v1 = {1, 2, 3};
vector<int> v2 = {0, 3, 9};
vector<int>::iterator itr = find(v2.begin(), v2.end(), 3);
itr = v2.erase(itr);
// like to insert "3", "2" and "1" to the same position which ends up "1", "2" and "3"
for (auto ri = v1.rbegin(); ri != v1.rend(); ++ri) {
v2.insert(itr, *ri);
}
for (const auto &i : v2) {
cout << i << " ";
}
cout << endl;
return 0;
}
的上面的代码崩溃。
是的,我知道像transform()或copy()这样的其他STL API可能是一个可以使用的API,但只是想知道上面的代码有什么问题?
http://www.cplusplus.com/reference/vector/vector/insert/请注意insert(...)函数是如何被重载的 - 您绝对可以将它转换为标准库的一行代码。 – druckermanly 2015-02-12 05:21:49