首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::list

Defined in header <list>

template< class T, class Allocator = std::allocator<T> > class list;

(1)

namespace pmr { template <class T> using list = std::list<T, std::pmr::polymorphic_allocator<T>>; }

(2)

(since C++17)

std::list是一个容器,它支持从容器中的任何位置插入和删除元素。不支持快速随机访问。它通常作为一个双链接列表来实现。相比较std::forward_list该容器提供双向迭代功能,但空间效率较低。

此外,删除和移动列表中的元素或跨几个列表,并不会使迭代器或引用失效。只有在删除相应的元素时,迭代器才会失效。

std::list满足…的要求Container,,,AllocatorAwareContainer,,,SequenceContainerReversibleContainer...

模板参数

T

-

The type of the elements. T must meet the requirements of CopyAssignable and CopyConstructible. (until C++11) The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type is a complete type and meets the requirements of Erasable, but many member functions impose stricter requirements. (since C++11)(until C++17) The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type meets the requirements of Erasable, but many member functions impose stricter requirements. This container (but not its members) can be instantiated with an incomplete element type if the allocator satisfies the allocator completeness requirements. (since C++17)

T must meet the requirements of CopyAssignable and CopyConstructible.

(until C++11)

The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type is a complete type and meets the requirements of Erasable, but many member functions impose stricter requirements.

(since C++11)(until C++17)

The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type meets the requirements of Erasable, but many member functions impose stricter requirements. This container (but not its members) can be instantiated with an incomplete element type if the allocator satisfies the allocator completeness requirements.

(since C++17)

T must meet the requirements of CopyAssignable and CopyConstructible.

(until C++11)

The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type is a complete type and meets the requirements of Erasable, but many member functions impose stricter requirements.

(since C++11)(until C++17)

The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type meets the requirements of Erasable, but many member functions impose stricter requirements. This container (but not its members) can be instantiated with an incomplete element type if the allocator satisfies the allocator completeness requirements.

(since C++17)

Allocator

-

An allocator that is used to acquire/release memory and to construct/destroy the elements in that memory. The type must meet the requirements of Allocator. The behavior is undefined if Allocator::value_type is not the same as T.

成员类型

Member type

Definition

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)

Allocator::reference

(until C++11)

value_type&

(since C++11)

Allocator::reference

(until C++11)

value_type&

(since C++11)

const_reference

Allocator::const_reference (until C++11) const value_type& (since C++11)

Allocator::const_reference

(until C++11)

const value_type&

(since C++11)

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)

Allocator::pointer

(until C++11)

std::allocator_traits<Allocator>::pointer

(since C++11)

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)

Allocator::const_pointer

(until C++11)

std::allocator_traits<Allocator>::const_pointer

(since C++11)

Allocator::const_pointer

(until C++11)

std::allocator_traits<Allocator>::const_pointer

(since C++11)

iterator

BidirectionalIterator

const_iterator

Constant bidirectional iterator

reverse_iterator

std::reverse_iterator<iterator>

const_reverse_iterator

std::reverse_iterator<const_iterator>

成员函数

(constructor)

constructs the list (public member function)

(destructor)

destructs the list (public member function)

operator=

assigns values to the container (public member function)

assign

assigns values to the container (public member function)

get_allocator

returns the associated allocator (public member function)

元素存取

前端访问第一个元素%28公共成员函数%29

返回访问最后一个元素%28公共成员函数%29

迭代器

BEGINCBEGIN将迭代器返回到开头%28的公共成员函数%29

End cend将迭代器返回到End%28公共成员函数%29

将反向迭代器返回到开头%28的公共成员函数%29

rend crend将反向迭代器返回到End%28公共成员函数%29

容量

空检查容器是否为空%28公共成员函数%29

Size返回元素数%28公共成员函数%29

马克斯[医]Size返回元素的最大可能数%28公共成员函数%29

修饰符

清除内容%28公共成员功能%29

插入元素%28公共成员函数%29

嵌入%28C++11%29构造元素就地%28公共成员函数%29

擦除元素%28公共成员函数%29

推[医]向末尾%28公共成员函数%29添加一个元素

座落[医]Back%28C++11%29在%28公共成员函数%29的末尾构造一个就地元素。

波普[医]回移除最后一个元素%28公共成员函数%29

推[医]前端插入元素到开头%28公共成员函数%29

座落[医]前端%28c++11%29在开始处构造一个元素(%28公共成员函数%29)。

波普[医]前端移除第一个元素%28公共成员函数%29

调整大小更改存储的元素数%28公共成员函数%29

交换交换内容%28公共成员函数%29

操作

合并两个排序列表%28公共成员函数%29

Splice从另一个列表中移动元素%28公共成员函数%29

移除[医]如果删除满足特定条件的元素%28公共成员函数%29

反转元素%28公共成员函数%29的顺序

唯一删除连续重复元素%28公共成员函数%29

排序元素%28公共成员函数%29

非会员职能

operator==operator!=operator<operator<=operator>operator>=

lexicographically compares the values in the list (function template)

std::swap(std::list)

specializes the std::swap algorithm (function template)

二次

代码语言:javascript
复制
#include <algorithm>
#include <iostream>
#include <list>
 
int main()
{
    // Create a list containing integers
    std::list<int> l = { 7, 5, 16, 8 };
 
    // 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 before 16 by searching
    auto it = std::find(l.begin(), l.end(), 16);
    if (it != l.end()) {
        l.insert(it, 42);
    }
 
    // Iterate and print values of the list
    for (int n : l) {
        std::cout << n << '\n';
    }
}

二次

产出:

二次

代码语言:javascript
复制
25
7
5
42
16
8
13

二次

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券