开发者社区> 问答> 正文

STL inserter插入器插入过程

#include
#include
#include
using namespace std;
int main()
{
listlst;
for(int i=1;i lst.push_back(i);
listlst2;
copy(lst.begin(),lst.end(),inserter(lst2,lst2.begin()));
cout<<"lst2:begin(): ";
for(auto it=lst2.begin();it!=lst2.end();it++)
cout<<*it<<ends;
cout<<endl;
system("pause");
}

为何输出为1 2 3 4而不是4 3 2 1,不是说inserter插入器要把元素插在第二个参数指定的元素之前么?
本人理解一是第一次插入结果1,第二次应该为2,1...故有上述问题
二是inserter插入器调用了insert(p,b,e)版本,则无此问题
求大神解疑

展开
收起
a123456678 2016-03-06 13:26:43 1806 0
1 条回答
写回答
取消 提交回答
  • push_bach函数:
    函数原型:
    iterator push_bash();
    功能:在容器的尾端插入一个数据

    楼主的这段代码好像问题比较多,编译肯定会出错

    #include
    #include
    #include
    using namespace std;
    int main()
    {
        list lst;
        for(int i=1;i lst.push_back(i);
        list lst2;
        copy(lst.begin(), lst.end(), inserter(lst2,lst2.begin()));
        cout << "lst2:begin(): ";
    
        for(auto it=lst2.begin();it!=lst2.end();it++)
        cout << *it << ends;
    
        cout << endl;
        system("pause");
    }
    2019-07-17 18:54:26
    赞同 展开评论 打赏
问答分类:
C++
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载