文章目录
- 一、元素操作
- 1、首尾 添加 / 删除 元素
- 2、获取 首尾 元素
- 二、迭代器遍历容器
- 1、正向迭代与反向迭代
- 2、代码示例
一、元素操作
1、首尾 添加 / 删除 元素
list 双向链表容器 提供了 push_back、pop_back、push_front 和 pop_front 等一系列用于操作列表元素的成员函数 , 函数原型如下 :
- 头部插入元素 : 在容器的头部插入 val 引用指向的值 ;
| void push_front (const value_type& val); |
| |
| |
| lstInt.push_front(666); |
- 头部删除元素 : 函数删除列表的第一个元素 , 如果列表为空 , 则此操作未定义崩溃退出 ;
| void pop_front (); |
| |
| |
| lstInt.pop_front(); |
- 尾部插入元素 : 在容器尾部插入一个元素 val ;
| void push_back (const value_type& val); |
| |
| |
| lstInt.push_back(888); |
| void pop_back (); |
| |
| |
| lstInt.pop_back(); |
上述函数都接受常量引用作为参数( 对于 push_back 和 push_front )或 没有参数(对于 pop_back 和 pop_front) , 并且没有返回值 ;
如果要删除元素 , 确保容器不为空 , 否则会出现 操作未定义 , 程序直接崩溃退出 ;
代码示例 :
| #include "iostream" |
| using namespace std; |
| #include "list" |
| |
| |
| void printL(list<int>& lst) { |
| |
| list<int>::iterator it = lst.begin(); |
| |
| cout << "list 容器内容 : "; |
| |
| |
| while (it != lst.end()) |
| { |
| |
| cout << *it << " "; |
| |
| it++; |
| } |
| |
| cout << endl; |
| } |
| |
| int main() { |
| |
| |
| list<int> lstInt{1, 2, 3, 4, 5}; |
| |
| |
| printL(lstInt); |
| |
| |
| lstInt.push_front(666); |
| |
| |
| lstInt.push_back(888); |
| |
| |
| printL(lstInt); |
| |
| |
| lstInt.pop_front(); |
| |
| |
| lstInt.pop_back(); |
| |
| |
| printL(lstInt); |
| |
| |
| |
| system("pause"); |
| |
| return 0; |
| }; |
执行结果 :
| list 容器内容 : 1 2 3 4 5 |
| list 容器内容 : 666 1 2 3 4 5 888 |
| list 容器内容 : 1 2 3 4 5 |
| 请按任意键继续. . . |

2、获取 首尾 元素
std::list 是一个双向链表容器 提供了 back 和 front 这两个成员函数 , 用于访问链表的最后一个和第一个元素 , 函数原型如下 :
- 访问首元素 : 该函数返回对链表第一个元素的引用 ; 如果链表为空 , 则此操作未定义 , 崩溃退出 ;
| reference front(); |
| const_reference front() const; |
- 访问尾元素 : 该函数返回对链表最后一个元素的引用 ; 如果链表为空 , 则此操作未定义 , 崩溃退出 ;
| reference back(); |
| const_reference back() const; |
代码示例 :
| // list 双向链表容器 使用初始化列表构造 |
| list<int> lstInt{1, 2, 3, 4, 5}; |
| |
| cout << "首元素 : " << lstInt.front() << endl; |
| cout << "尾元素 : " << lstInt.back() << endl; |
| |
完整代码示例 :
| #include "iostream" |
| using namespace std; |
| #include "list" |
| |
| |
| void printL(list<int>& lst) { |
| |
| list<int>::iterator it = lst.begin(); |
| |
| cout << "list 容器内容 : "; |
| |
| |
| while (it != lst.end()) |
| { |
| |
| cout << *it << " "; |
| |
| it++; |
| } |
| |
| cout << endl; |
| } |
| |
| int main() { |
| |
| |
| list<int> lstInt{1, 2, 3, 4, 5}; |
| |
| |
| printL(lstInt); |
| |
| cout << "首元素 : " << lstInt.front() << endl; |
| cout << "尾元素 : " << lstInt.back() << endl; |
| |
| |
| |
| system("pause"); |
| |
| return 0; |
| }; |
执行结果 :
| list 容器内容 : 1 2 3 4 5 |
| 首元素 : 1 |
| 尾元素 : 5 |
| 请按任意键继续. . . |

二、迭代器遍历容器
1、正向迭代与反向迭代
std::list 双向链表容器 提供了 begin、end、rbegin 和 rend 这几个成员函数,用于 获取 迭代访问链表中的元素 的 迭代器 , 函数原型如下 :
- 获取首元素迭代器 : 返回一个迭代器 , 指向链表的第一个元素 ;
| iterator begin(); |
| const_iterator begin() const; |
- 获取尾元素之后的迭代器 : 返回一个迭代器 , 指向链表的尾部 , 该尾部指的是 超出链表末尾 的位置 , 不是最后一个元素 , 是最后一个元素后面的位置 , 无法获取值 ;
| iterator end(); |
| const_iterator end() const; |
- 获取指向尾元素的反向迭代器 : 该函数返回一个反向迭代器 , 指向链表的最后一个元素 ; 如果链表为空 , 则此操作未定义 ; 反向迭代器从链表的尾部向头部移动 ;
- 获取指向首元素之前的反向迭代器 : 返回一个反向迭代器 , 指向链表的 超出头部 ”的位置 , 即第一个元素的前一个位置 ; 该迭代器 它用于与 rbegin 一起实现完整的逆向迭代 ;
| reverse_iterator rend(); |
| const_reverse_iterator rend() const; |
迭代器的位置如下图所示 :

| |
| for (list<int>::iterator it = lstInt.begin(); it != lstInt.end(); it++) |
| { |
| cout << *it << " "; |
| } |
| cout << endl; |
| |
| for (list<int>::reverse_iterator rit = lstInt.rbegin(); rit != lstInt.rend(); rit++) |
| { |
| cout << *rit << " "; |
| } |
| cout << endl; |
2、代码示例
代码示例 :
| #include "iostream" |
| using namespace std; |
| #include "list" |
| |
| |
| void printL(list<int>& lst) { |
| |
| list<int>::iterator it = lst.begin(); |
| |
| cout << "list 容器内容 : "; |
| |
| |
| while (it != lst.end()) |
| { |
| |
| cout << *it << " "; |
| |
| it++; |
| } |
| |
| cout << endl; |
| } |
| |
| int main() { |
| |
| |
| list<int> lstInt{1, 2, 3, 4, 5}; |
| |
| |
| for (list<int>::iterator it = lstInt.begin(); it != lstInt.end(); it++) |
| { |
| cout << *it << " "; |
| } |
| cout << endl; |
| |
| |
| for (list<int>::reverse_iterator rit = lstInt.rbegin(); rit != lstInt.rend(); rit++) |
| { |
| cout << *rit << " "; |
| } |
| cout << endl; |
| |
| |
| |
| system("pause"); |
| |
| return 0; |
| }; |
执行结果 :
| 1 2 3 4 5 |
| 5 4 3 2 1 |
| 请按任意键继续. . . |
