序列容器之list

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.

  • 模板参数
TThe type of the elements.
 

 (until C++11)

T must meet the requirements of CopyAssignable and CopyConstructible.  

 (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 is a complete type and meets the requirements of Erasable, but many member functions impose stricter requirements.  

(since 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.    

成员类型

成员类型定义
value_type
allocator_typeAllocator
size_typeUnsigned 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) 

pointerAllocator::pointer    (until C++11)    std::allocator_traits<Allocator>::pointer    (since C++11) 
const_pointerAllocator::const_pointer    (until C++11)   std::allocator_traits<Allocator>::const_pointer (since C++11) 
reverse_iteratorstd::reverse_iterator<iterator>
const_reverse_iterator std::reverse_iterator<const_iterator>
  • 元素访问
frontaccess the first element
(public member function)
backaccess 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)
  • 指标
emptychecks whether the container is empty
(public member function)
sizereturns the number of elements
(public member function)
max_sizereturns 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)    
(until C++11)iterator insert( iterator pos, const T& value );
(since C++11)iterator insert( const_iterator pos, const T& value );

(2) 
 (since C++11)iterator insert( const_iterator pos, T&& value );
  (3)    
(until C++11)void insert( iterator pos, size_type count, const T& value );
(since C++11)iterator insert( const_iterator pos, size_type count, const T& value );
(4)    
(until C++11)template< class InputIt >void insert( iterator pos, InputIt first, InputIt last);
(since C++11)template< class InputIt >iterator insert( const_iterator pos, InputIt first, InputIt last );
(5)    (since C++11)
iterator insert( const_iterator pos, std::initializer_list<T> ilist );

inserts elements
(public member function)
emplacetemplate< 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_backvoid pop_back();removes the last element
(public member function)
push_frontvoid 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_frontvoid 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)
swapvoid 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 );
(since C++11)void merge( list&& other );
(2)    
template <class Compare>
void merge( list& other, Compare comp );
 template <class Compare>
(since C++11)void merge( list&& other, Compare comp );

merges two sorted lists
(public member function)
splice

(1)    

void splice( const_iterator pos, list& other );
(since C++11)void splice( const_iterator pos, list&& other );
(2)    
void splice( const_iterator pos, list& other, const_iterator it ); 
(since C++11)void splice( const_iterator pos, list&& other, const_iterator it );
(3)    
void splice( const_iterator pos, list& other,
    const_iterator first, const_iterator last);
(since C++11)void splice( const_iterator pos, list&& other, const_iterator first, const_iterator last );

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)
reversevoid 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)
size_type unique();(since C++20)
(2)    
(until C++20)template< class BinaryPredicate >
void unique( BinaryPredicate p );
(since C++20)template< class BinaryPredicate >
size_type unique( BinaryPredicate p );

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值