首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将列表插入到列表的向量中std::vector<std::list<unsigned>>

将列表插入到列表的向量中std::vector<std::list<unsigned>>
EN

Stack Overflow用户
提问于 2021-03-17 08:42:57
回答 1查看 129关注 0票数 1

我试图使用迭代器插入将一个列表插入到列表向量中(const_iterator position,InputIterator first,InputIterator last);

代码语言:javascript
复制
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构建文件时,我会得到以下两个错误

  1. /usr/lib/llvm-11/bin/../include/c++/v1/algorithm:1701:19:error: no viable overloaded '=' *__result = *__first;
  2. /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错误。它建造得很成功,但试验失败了。

代码语言:javascript
复制
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.

EN

回答 1

Stack Overflow用户

发布于 2021-03-17 08:57:27

代码语言:javascript
复制
output.insert(output.end(), sortedList.begin(), sortedList.end());

这将在向量的末尾附加一系列元素(在迭代器之间)。如果你有--例如-- std::vector<unsigned> outputstd::list<unsigned> sortedList,你可以这么做。但是,您希望将列表作为一个整体插入到列表向量中。然后,列表将是一个元素。

代码语言:javascript
复制
output.insert(output.end(), sortedList); // <-- copy/move insertion

看起来有点尴尬。更好利用

代码语言:javascript
复制
output.push_back(sortedList);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66669590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档