c++ list<>
时间: 2023-12-09 16:03:47 浏览: 83
C++ list的实例详解
5星 · 资源好评率100%
C++中的list<>是一个双向链表容器,可以在任何位置高效地插入和删除元素。以下是两个list<>的例子:
1.遍历list<>并输出元素
```c++
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> list1 = {12, 23, 34};
list<int>::const_iterator it1 = list1.begin();
for (; it1 != list1.end(); it1++) {
cout << *it1 << "\t";
}
cout << endl;
list<int>::reverse_iterator it2 = list1.rbegin();
for (; it2 != list1.rend(); it2++) {
cout << *it2 << "\t";
}
cout << endl;
return 0;
}
```
2.访问list<>的首尾元素
```c++
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> list1 = {12, 23, 34};
cout << "list1.front(): " << list1.front() << endl;
cout << "list1.back(): " << list1.back() << endl;
return 0;
}
```
阅读全文