#include <iostream> #include <list> using namespace std; int main() { std::list<int> mylist{ 1,2,3,4 }; int &first = mylist.front(); int &last = mylist.back(); cout << first << " " << last << endl; first = 10; last = 20; cout << mylist.front() << " " << mylist.back() << endl; return 0; }输出结果为:
1 4
10 20
#include <iostream> #include <list> using namespace std; int main() { const std::list<int> mylist{1,2,3,4,5}; auto it = mylist.begin(); cout << *it << " "; ++it; while (it!=mylist.end()) { cout << *it << " "; ++it; } return 0; }运行结果为:
1 2 3 4 5
值得一提的是,对于非 const 类型的 list 容器,迭代器不仅可以访问容器中的元素,也可以对指定元素的值进行修改。
本文链接:http://task.lmcjl.com/news/14403.html