#include <iostream>
#include <list>
using namespace std;
#include <iterator>
int main()
{
list<int> o;
o.push_back(1);
o.push_back(2);
o.push_back(3);
list<int>::iterator listIt;
for (listIt=o.begin(); listIt!=o.end(); ++listIt)
{
cout<<*listIt<<" ";
}
cout<<endl;
o.push_front(4);
o.push_front(5);
o.push_front(6);
for (listIt=o.begin(); listIt!=o.end(); ++listIt)
{
cout<<*listIt<<" ";
}
cout<<endl;
o.insert(++o.begin(), 7);
o.insert(o.end(), 20);
for (listIt=o.begin(); listIt!=o.end(); ++listIt)
{
cout<<*listIt<<" ";
}
cout<<endl;
return 0;
}
输出:
1 2 3
6 5 4 1 2 3
6 7 5 4 1 2 3 20
主要是注意一下insert操作,第一个参数是个指针,但是最多也就是++o.begin,
不能o.begin+2或者什么。
在几个帖子上看了看,好像链表不支持在中间插入,会破坏链表的连贯性还是什么。
如果有人知道,请告诉我,我也想学习一下
本文详细介绍了使用C++标准库中的list容器进行各种操作的方法,包括元素的添加、遍历及特定位置插入等,并通过实例展示了如何正确使用这些功能。
479

被折叠的 条评论
为什么被折叠?



