定义于头文件
| template< class T, class Allocator = std::allocator | (1) | |
| namespace pmr { template using list = std::list | (2) | (C++17 起) |
std::list 是支持常数时间从容器任何位置插入和移除元素的容器。不支持快速随机访问。它通常实现为双向链表。与 std::forward_list 相比,此容器提供双向迭代但在空间上效率稍低。
在 list 内或在数个 list 间添加、移除和移动元素不会非法化迭代器或引用。迭代器仅在对应元素被删除时非法化。
std::list 满足容器 (Container) 、具分配器容器 (AllocatorAwareContainer) 、序列容器 (SequenceContainer) 及可逆容器 (ReversibleContainer) 的要求。
- std::list<T,Allocator>::begin,
- std::list<T,Allocator>::cbegin
| iterator begin(); | (C++11 前) | |
| iterator begin() noexcept; | (C++11 起) | |
| const_iterator begin() const; | (C++11 前) | |
| const_iterator begin() const noexcept; | (C++11 起) | |
| const_iterator cbegin() const noexcept; | (C++11 起) |
返回指向容器首元素的迭代器。
若容器为空,则返回的迭代器将等于 end() 。

(无)
指向首元素的迭代器。
常数。
- std::list<T,Allocator>::end,
- std::list<T,Allocator>::cend
| iterator end(); | (C++11 前) | |
| iterator end() noexcept; | (C++11 起) | |
| const_iterator end() const; | (C++11 前) | |
| const_iterator end() const noexcept; | (C++11 起) | |
| const_iterator cend() const noexcept; | (C++11 起) |
返回指向容器末元素后一元素的迭代器。
此元素表现为占位符;试图访问它导致未定义行为。

(无)
指向后随最后元素的迭代器。
常数。
- std::list<T,Allocator>::rbegin,
- std::list<T,Allocator>::crbegin
| reverse_iterator rbegin(); | (C++11 前) | |
| reverse_iterator rbegin() noexcept; | (C++11 起) | |
| const_reverse_iterator rbegin() const; | (C++11 前) | |
| const_reverse_iterator rbegin() const noexcept; | (C++11 起) | |
| const_reverse_iterator crbegin() const noexcept; | (C++11 起) |
返回指向逆向容器首元素的逆向迭代器。它对应非逆向容器的末元素。
(无)
指向首元素的逆向迭代器。
常数。
- std::list<T,Allocator>::rend,
- std::list<T,Allocator>::crend
| reverse_iterator rend(); | (C++11 前) | |
| reverse_iterator rend() noexcept; | (C++11 起) | |
| const_reverse_iterator rend() const; | (C++11 前) | |
| const_reverse_iterator rend() const noexcept; | (C++11 起) | |
| const_reverse_iterator crend() const noexcept; | (C++11 起) |
返回指向逆向容器末元素后一元素的逆向迭代器。它对应非逆向容器首元素的前一元素。此元素表现为占位符,试图访问它导致未定义行为。

(无)
指向末元素后一元素的逆向迭代器。
常数。
- #include <iostream>
- #include <list>
-
- template<typename T>
- std::ostream& operator<<(std::ostream& s, const std::list<T>& v)
- {
- s.put('[');
- for (const auto& e : v)
- {
- s << e << " ";
- }
- return s << ']';
- }
-
- int main()
- {
- std::list<char> strings {'a', 'b', 'c', 'd', 'e', 'f'};
- std::cout << "original strings: " << strings << std::endl;
-
- //iterator 可修改容器元素
- for (std::list<char>::iterator it = strings.begin(); it != strings.end(); it++)
- {
- *it += 6;
- }
-
- //iterator 遍历元素,打印输出
- std::cout << "iterator *it += 6 strings: [";
- for (std::list<char>::iterator it = strings.begin(); it != strings.end(); it++)
- {
- std::cout << *it << " ";
- }
- std::cout << "]" << std::endl;
-
-
- //const_iterator 不可修改容器元素
- // for (std::list<char>::const_iterator it = strings.cbegin(); it != strings.cend(); it++)
- // {
- // // 编译失败
- // *it += 6;
- // }
-
- //const_iterator 遍历元素,打印输出
- std::cout << "const_iterator strings: [";
- for (std::list<char>::const_iterator it = strings.cbegin(); it != strings.cend(); it++)
- {
- std::cout << *it << " ";
- }
- std::cout << "]" << std::endl;
-
-
- //reverse_iterator 可修改容器元素
- for (std::list<char>::reverse_iterator it = strings.rbegin(); it != strings.rend(); it++)
- {
- *it += 6;
- }
-
- //reverse_iterator 逆向遍历元素,打印输出
- std::cout << "reverse_iterator *it += 6 strings: [";
- for (std::list<char>::reverse_iterator it = strings.rbegin(); it != strings.rend(); it++)
- {
- std::cout << *it << " ";
- }
- std::cout << "]" << std::endl;
-
- //const_reverse_iterator 不可修改容器元素
- // for (std::list<char>::const_reverse_iterator it = strings.crbegin(); it != strings.crend(); it++)
- // {
- // // 编译失败
- // *it += 6;
- // }
- }
