功能描述:
- 对list容器中数据进行存取
函数原型:
- front(); //返回第一个元素
- back(); //返回最后一个元素
代码演示:
#include<iostream>#include<list>using namespace std;//list容器的数据存取//list的迭代器不支持随机访问void test01() {//创建list容器list<int>L1;//默认构造L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);cout << "第一个元素为:" << L1.front() << endl;cout << "最后一个元素为:" << L1.back() << endl;list<int>::const_iterator it = L1.begin();it++;//支持双向it--;//it = it + 1;//报错, 不支持随机访问cout << *it<< endl;}int main() {test01();return 0;}
运行结果:
