Sky*_*sis 3 c++ gcc iterator llvm clang
我正在尝试制作一个字符串向量的副本,并将其附加到其原始向量的末尾,即复制其内容.例:
Input : vector<string> s = {"abc", "def"}
Output: vector<string> s = {"abc", "def", "abc", "def"}
Run Code Online (Sandbox Code Playgroud)
我正在使用插入方法,即
s.insert(s.end(), s.begin(), s.end());
Run Code Online (Sandbox Code Playgroud)
但是,这表现出与编译器相关的结果.在,LLVM clang,它给了我预期的答案.
GCC给了我
Output: vector<string> s = {"abc", "def", "", ""}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么会发生这种情况以及实现此向量复制目标最安全的方法是什么?
以下是该程序的ideone.com链接:http://ideone.com/40CH8q
尽管可以使用迭代器完成,但安全的替代方法是避免它们:
size_t size = v.size(); // Of course we shouldn't access .size() in the loop...
v.reserve(size * 2); // Preallocation. Thanks @Ali for this performance hint
for (size_t i = 0; i < size; ++i)
v.push_back(v[i]);
Run Code Online (Sandbox Code Playgroud)
通常,使用迭代器同时修改数据结构(不仅仅是其元素)是危险的; 当迭代器失效时以及在修改后重用旧迭代器是安全的时候,你应该仔细阅读.因此,有时使用"旧"方法迭代随机访问序列是有意义的:使用索引变量.
正如其他人已经指出的那样,您需要通过调用来确保vector::reserve()
向量在插入期间不会被重新分配。push_back()
(如果您选择将元素放入向量中,那么调用reserve也是一个好主意。)
然后仍然存在迭代器失效问题(详见vector::insert()
),但据我所知,下面的代码绕过了这个问题:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> s{"abc", "def"};
auto size = s.size();
s.reserve(2*size); // <-- This one is essential to ensure
// no reallocation during insertion
const string* first = s.data(); // using pointer, instead of iterator
copy(first, first+size, back_inserter(s));
for (const auto& e : s)
cout << e << '\t';
cout << endl;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1026 次 |
最近记录: |