#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)版本,则无此问题
求大神解疑
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");
}
http://blog.csdn.net/bxyill/article/details/7984631
通过inserter在it指定位置之前插完一个数值后,
插入迭代器it仍会指向原来的位置,
相当于调用了:
it= list2.insert(it,val);
++it;
(参考C++primer 第五版 p358)
list2为空列表,由inserter返回的插入迭代器指向不存在的end
所以等效在end前依次添加了:1,2,3,4