C++ STL-List函数用法整理

定义:

      list 是C++标准程式库中的一个,可以简单视之为双向连结串列,以线性列的方式管理物件集合。list 的特色是在集合的任何位置增加或删除元素都很快,但是不支持随机存取。list 是C++标准程式库提供的众多容器(container)之一,除此之外还有vector、set、map、…等等。list 以模板方式实现(即泛型),可以处理任意型别的变数,包括使用者自定义的资料型态,例如:它可以是一个放置整数(int)型态的 list、也可以是放置字串(char 或 string)型态的 list、或者放置使用者自定类别(user-defined class)的 list。(来自维基百科)

接下来我们说一下它的用法

头文件

    声明前首先需要头文件#include<list>。

构造

list();
explicit list( const Allocator& alloc );

(1)  
  (2)  
explicit list( size_type count, const T& value = T(),const Allocator& alloc = Allocator()); (C++11 前)
         list( size_type count, const T& value,const Allocator& alloc = Allocator()); (C++11 起)
  (3)  

explicit list( size_type count );

(C++11 起) 
(C++14 前)

explicit list( size_type count, const Allocator& alloc = Allocator() );

(C++14 起)
template< class InputIt >

list( InputIt first, InputIt last,  const Allocator& alloc = Allocator() );

(4)  

list( const list& other );

(5)  

list( const list& other, const Allocator& alloc );

(5) (C++11 起)

list( list&& other );

(6) (C++11 起)

list( list&& other, const Allocator& alloc );

扫描二维码关注公众号,回复: 4033231 查看本文章
(7) (C++11 起)

list( std::initializer_list<T> init, const Allocator& alloc = Allocator() );

(8) (C++11 起)
     

从各种数据源构造新容器,可选地使用用户提供的分配器 alloc 。

1) 默认构造函数。构造空容器。若不提供分配器,则从默认构造的实例获得分配器。

2) 构造拥有 count 个有值 value 的元素的容器。

3) 构造拥有个 count 默认插入的 T 实例的容器。不进行复制。

4) 构造拥有范围 [first, last) 内容的容器。

若 InputIt 是整数类型,则此构造函数拥有的效果同 list(static_cast<size_type>(first), static_cast<value_type>(last), a) 。

(C++11 前)

此重载仅若InputIt 满足输入迭代器 (InputIterator) 才参与重载决议,以避免和重载 (2) 的歧义。

(C++11 起)

5) 复制构造函数。构造拥有 other 内容的容器。若不提供 alloc ,则如同通过调用 std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator())获得分配器。

6) 移动构造函数。用移动语义构造拥有 other 内容的容器。分配器通过属于 other 的分配器移动构造获得。

7) 有分配器扩展的移动构造函数。以 alloc 为新容器的分配器,从 other 移动内容;若 alloc != other.get_allocator() ,则它导致逐元素移动。

8) 构造拥有 initializer_list init 内容的容器。

参数

alloc - 用于此容器所有内存分配的分配器
count - 容器的大小
value - 以之初始化容器元素的值
first, last - 复制元素的来源范围
other - 用作初始化容器元素来源的另一容器
init - 用作初始化元素来源的 initializer_list

复杂度

1) 常数

2-3) 与 count 成线性

4) 与 first 和 last 的距离成线性

5) 与 other 的大小成线性

6) 常数。

7) 若 alloc != other.get_allocator() 则为线性,否则为常数。

8) 与 init 的大小成线性。

成员函数

元素访问

front

访问第一个元素 
(公开成员函数)

back

访问最后一个元素 
(公开成员函数)

迭代器

begin
cbegin

返回指向容器第一个元素的迭代器 
(公开成员函数)

end 
cend

返回指向容器尾端的迭代器 
(公开成员函数)

rbegin
crbegin

返回指向容器最后元素的逆向迭代器 
(公开成员函数)

rend
crend

返回指向前端的逆向迭代器 
(公开成员函数)

容量

empty

检查容器是否为空 
(公开成员函数)

size

返回容纳的元素数 
(公开成员函数)

max_size

返回可容纳的最大元素数 
(公开成员函数)

修改器

clear

清除内容 
(公开成员函数)

insert

插入元素 
(公开成员函数)

emplace

(C++11)

原位构造元素 
(公开成员函数)

erase

擦除元素 
(公开成员函数)

push_back

将元素添加到容器末尾 
(公开成员函数)

emplace_back

(C++11)

在容器末尾就地构造元素 
(公开成员函数)

pop_back

移除末元素 
(公开成员函数)

push_front

插入元素到容器起始 
(公开成员函数)

emplace_front

(C++11)

在容器头部就地构造元素 
(公开成员函数)

pop_front

移除首元素 
(公开成员函数)

resize

改变容器中可存储元素的个数 
(公开成员函数)

swap

交换内容 
(公开成员函数)

操作

merge

合并二个已排序列表 
(公开成员函数)

splice

从另一个list中移动元素 
(公开成员函数)

remove
remove_if

移除满足特定标准的元素 
(公开成员函数)

reverse

将该链表的所有元素的顺序反转 
(公开成员函数)

unique

删除连续的重复元素 
(公开成员函数)

sort

对元素进行排序 (需要重载运算符)

非成员函数

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

按照字典顺序比较 list 中的值 
(函数模板)

std::swap(std::list)

特化 std::swap 算法 

程序示例 

#include <algorithm>
#include <iostream>
#include <list>
 
int main()
{
    // 创建含整数的 list
    std::list<int> l = { 7, 5, 16, 8 };
 
    // 添加整数到 list 开头
    l.push_front(25);
    // 添加整数到 list 结尾
    l.push_back(13);
 
    // 以搜索插入 16 前的值
    auto it = std::find(l.begin(), l.end(), 16);
    if (it != l.end()) {
        l.insert(it, 42);
    }
 
    // 迭代并打印 list 的值
    for (int n : l) {
        std::cout << n << '\n';
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41785863/article/details/83959280