#include <iostream>
#include <list>
using namespace std;
void printList (const list<int> &l)
{
for (list<int>::const_iterator it = l.begin(); it != l.end(); it ++)
{
cout << *it << " ";
}
cout << endl;
}
void test01 ()
{
list<int> l;
l.push_back(10);
l.push_back(20);
l.push_back(30);
l.push_back(40);
//不能用[]访问list中的元素
//不能用at访问list中的元素
//因为list本质是一个链表,不是一块连续的空间存储的,迭代器也不支持随机访问
cout << l.front() << endl;
cout << l.back() << endl;
list<int>::iterator it = l.begin();
it++;
cout << *it << endl;
//不支持it = it + 1,不支持随机访问
}
int main ()
{
test01();
return 0;
}
30、list容器-数据存取
最新推荐文章于 2024-07-25 21:43:46 发布