https://en.cppreference.com/w/cpp/container
- 头文件
<list>
- 声明
(1)
template<
class T,
class Allocator = std::allocator<T>
> class list;
(2) (since C++17)
namespace pmr {
template <class T>
using list = std::list<T, std::pmr::polymorphic_allocator<T>>;
}
- 描述
std::list是一个容器,它支持从容器内以固定时间的插入和删除元素。它不支持快速随机访问。它通常以双向链表的方式实现。与std::forward_list相比,它提供了双向迭代的能力,当然需要的内存空间要大一些。
此外,删除或移动链表中的元素,或跨越多个链表,都不会使得指向其它元素的迭代器或引用非法;只有当迭代器指向当前删除的元素时,才是非法的。
std::list meets the requirements of Container, AllocatorAwareContainer, SequenceContainer and ReversibleContainer.
- 模板参数
T | The type of the elements. |
(until C++11) T must meet the requirements of CopyAssignable and CopyConstructible. (since C++11)(until C++17) (since C++17) |
成员类型
成员类型 | 定义 |
value_type | T |
allocator_type | Allocator |
size_type | Unsigned integer type (usually std::size_t) |
difference_type | Signed integer type (usually std::ptrdiff_t) |
reference | Allocator::reference (until C++11) value_type& (since C++11) |
const_reference | Allocator::const_reference (until C++11) const value_type& (since C++11) |
pointer | Allocator::pointer (until C++11) std::allocator_traits<Allocator>::pointer (since C++11) |
const_pointer | Allocator::const_pointer (until C++11) std::allocator_traits<Allocator>::const_pointer (since C++11) |
reverse_iterator | std::reverse_iterator<iterator> |
const_reverse_iterator | std::reverse_iterator<const_iterator> |
- 元素访问
front | access the first element (public member function) |
back | access the last element (public member function) |
- 迭代器
begin cbegin | (C++11) returns an iterator to the beginning (public member function) |
end cend | (C++11) returns an iterator to the end (public member function) |
rbegin crbegin | (C++11) returns a reverse iterator to the beginning (public member function) |
rend crend | (C++11) returns a reverse iterator to the end (public member function) |
- 指标
empty | checks whether the container is empty (public member function) |
size | returns the number of elements (public member function) |
max_size | returns the maximum possible number of elements (public member function) |
- 修改
clear | (until C++11)void clear(); (since C++11)void clear() noexcept; | clears the contents (public member function) |
insert | (1) (2) | inserts elements (public member function) |
emplace | template< class... Args > iterator emplace( const_iterator pos, Args&&... args ); (since C++11) | (C++11) constructs element in-place (public member function) |
erase | (1) (until C++11)iterator erase( iterator pos ); (since C++11)iterator erase( const_iterator pos ); (2) (until C++11)iterator erase( iterator first, iterator last ); (since C++11)iterator erase( const_iterator first, const_iterator last ); | erases elements (public member function) |
push_back | (1) void push_back( const T& value ); (2) (since C++11)void push_back( T&& value ); | adds an element to the end (public member function) |
emplace_back | (since C++11)(until C++17)void emplace_back( Args&&... args ); (since C++17)template< class... Args > reference emplace_back( Args&&... args ); | (C++11) constructs an element in-place at the end (public member function) |
pop_back | void pop_back(); | removes the last element (public member function) |
push_front | void push_front( const T& value ); void push_front( T&& value ); (since C++11) | inserts an element to the beginning (public member function) |
emplace_front | (since C++11)(until C++17)void emplace_front( Args&&... args ); (since C++17)template< class... Args > reference emplace_front( Args&&... args ); | (C++11) constructs an element in-place at the beginning (public member function) |
pop_front | void pop_front(); | removes the first element (public member function) |
resize | (until C++11)void resize( size_type count, T value = T() ); (1) (since C++11)void resize( size_type count ); (2) (since C++11)void resize( size_type count, const value_type& value ); | changes the number of elements stored (public member function) |
swap | void swap( list& other );(until C++17) void swap( list& other ) noexcept(/* see below */);(since C++17) | swaps the contents (public member function) |
- 操作
merge | (1) void merge( list& other ); | merges two sorted lists (public member function) |
splice | (1) void splice( const_iterator pos, list& other ); | moves elements from another list (public member function) |
remove remove_if | (until C++20)void remove( const T& value ); (since C++20)size_type remove( const T& value ); (until C++20)template< class UnaryPredicate >void remove_if( UnaryPredicate p ); (since C++20)template< class UnaryPredicate >size_type remove_if( UnaryPredicate p ); | removes elements satisfying specific criteria (public member function) |
reverse | void reverse();(until C++11) void reverse() noexcept;(since C++11) | reverses the order of the elements (public member function) |
unique | (1) void unique();(until C++20) | removes consecutive duplicate elements (public member function) |
sort | (1) void sort(); (2) template< class Compare > void sort( Compare comp ); | sorts the elements |
- 举例
#include <algorithm>
#include <iostream>
#include <list>
int main()
{
// Create a list containing integers
std::list<int> l = { 7, 5, 16, 8 , 1, 2, 16, 3,4, 6, 16};
// Add an integer to the front of the list
l.push_front(25);
// Add an integer to the back of the list
l.push_back(13);
// Insert an integer after 16 by searching
auto it = std::find(l.begin(), l.end(), 16);
while(it != l.end()) {
std::cout<<" :"<<*it<<std::endl;
it = l.insert(++it, 42);
std::cout<<" :"<<*it<<std::endl;
it = std::find(++it, l.end(), 16);
}
// Iterate and print values of the list
for (int n : l) {
std::cout << n << '\n';
}
std::cout << "*****************************"<< '\n';
//erase first 16
it = l.begin();
while(it != l.end()) {
if(*it == 16){
it = l.erase(it);
}
else{
it++;
}
}
// Iterate and print values of the list
for (int n : l) {
std::cout << n << '\n';
}
return 0;
}
:16
:42
:16
:42
:16
:42
25
7
5
16
42
8
1
2
16
42
3
4
6
16
42
13
*****************************
25
7
5
42
8
1
2
42
3
4
6
42
13