示例:
#include<iostream>
using namespace std;
#include<list>
void test01()
{
list<int>L1;
for (int i = 0; i < 5; i++)
{
L1.push_back((i * 10) + 10);
}
cout << "第一个元素为:" << L1.front() << endl;
cout << "最后一个元素为:" << L1.back() << endl;
//打印所有元素,通过迭代器方式访问
for (list<int>::iterator it = L1.begin(); it != L1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
return 0;
}
总结:list容器不支持使用【】和at方式访问数据
1.返回第一个元素 front();
2.返回最后一个个元素back();