我试图使用迭代器插入将一个列表插入到列表向量中(const_iterator position,InputIterator first,InputIterator last);
std::vector<std::list<unsigned>> output;
std::list<unsigned> originalFile = {2, 6, 3, 56, 4, 29, 9, 43, 8, 12,
76, 45, 90, 124, 23, 11, 56, 26, 80, 13};
auto iter = originalFile.begin(); //it has 20 positive int inside
std::list<unsigned> sortedList;
unsigned int i = 0;
unsigned int v = 0;
unsigned int numFiles = originalFile.size() / m;
unsigned int index = originalFile.size() % m;
if(index != 0){
numFiles += 1;
}
while(numFiles != v)
{
while(i != m)
{
sortedList.push_back(*iter);
i++;
iter++;
}
sortedList.sort();
// the error point to this line with the insert function
output.insert(output.end(),sortedList.begin(),sortedList.end());
v++;
i = 0;
sortedList.clear();
}当我用./build构建文件时,我会得到以下两个错误
/usr/lib/llvm-11/bin/../include/c++/v1/algorithm:1701:19:error: no viable overloaded '=' *__result = *__first;/usr/lib/llvm-11/bin/../include/c++/v1/algorithm:1710:12: error: no matching function for call to '__copy_constexpr' return __copy_constexpr(__first, __last, __result);我也尝试使用push_back,但是当我使用./运行gtest运行gtest时,会得到Segmentation fault错误。它建造得很成功,但试验失败了。
for(auto iterList = sortedList.begin(); iterList != sortedList.end(); iterList++)
{
output[v].push_back(*iterList);
}此外,我尝试使用上述代码与output.at(v).puch_back(*iterList),但成功地构建,它给我错误的C++ exception with description "vector" thrown in the test body.。
发布于 2021-03-17 08:57:27
output.insert(output.end(), sortedList.begin(), sortedList.end());这将在向量的末尾附加一系列元素(在迭代器之间)。如果你有--例如-- std::vector<unsigned> output和std::list<unsigned> sortedList,你可以这么做。但是,您希望将列表作为一个整体插入到列表向量中。然后,列表将是一个元素。
output.insert(output.end(), sortedList); // <-- copy/move insertion看起来有点尴尬。更好利用
output.push_back(sortedList);https://stackoverflow.com/questions/66669590
复制相似问题