SiriBlog

siriyang的个人博客


  • 首页

  • 排行榜

  • 标签115

  • 分类37

  • 归档320

  • 关于

  • 搜索

STL常用模板:List链表

发表于 2020-01-08 更新于 2021-10-29 分类于 计算机 , 技术 , C/C++ 阅读次数: Valine:
本文字数: 4.2k 阅读时长 ≈ 4 分钟

C/C++参考文档

概念

  List将元素按顺序储存在链表中。与向量(vector)相比, 它允许快速的插入和删除,但是随机访问却比较慢。

头文件

1
#include <list>

名字空间

1
std

构造函数

定义:

1
list<Type>

示例:

1
2
3
4
5
list<int> lst1;          //创建空list
list<int> lst2(5); //创建含有5个元素的list
list<int> lst3(3,2); //创建含有3个元素的list
list<int> lst4(lst2); //使用lst2初始化lst4
list<int> lst5(lst2.begin(),lst2.end()); //同lst4

常用函数

assign

语法:

1
2
void assign( input_iterator start, input_iterator end );
void assign( size_type num, const TYPE &val );

  assign()函数以迭代器start和end指示的范围为list赋值或者为list赋值num个以val为值的元素。


back

语法:

1
reference back();

  back()函数返回一个引用,指向list的最后一个元素。


begin

语法:

1
iterator begin();

  begin()函数返回一个迭代器,指向list的第一个元素。例如,

1
2
3
4
5
6
7
8
9
// 创建一个元素类型是字符的链表
list<char> charList;
for( int i=0; i < 10; i++ )
charList.push_front( i + 65 );

// 显示这个链表
list<char>::iterator theIterator;
for( theIterator = charList.begin(); theIterator != charList.end(); theIterator++ )
cout << *theIterator;

clear

语法:

1
void clear();

  clear()函数删除list的所有元素。


empty

语法:

1
bool empty();

  empty()函数返回真(true)如果链表为空,否则返回假。例如:

1
2
3
4
5
6
7
list<int> the_list;
for( int i = 0; i < 10; i++ )
the_list.push_back( i );
while( !the_list.empty() ) {
cout << the_list.front() << endl;
the_list.pop_front();
}

end

语法:

1
2
3
4
5
6
7
8
9
10
11
12
13
  iterator end();
```

&emsp;&emsp;`end()`函数返回一个迭代器,指向链表的末尾。

---

### erase
**语法:**

```C++
iterator erase( iterator pos );
iterator erase( iterator start, iterator end );

  erase()函数删除以pos指示位置的元素, 或者删除start和end之间的元素。 返回值是一个迭代器,指向最后一个被删除元素的下一个元素。


front

语法:

1
reference front();

  front()函数返回一个引用,指向链表的第一个元素。

1
2
3
4
5
6
7
list<int> the_list;
for( int i = 0; i < 10; i++ )
the_list.push_back( i );
while( !the_list.empty() ) {
cout << the_list.front() << endl;
the_list.pop_front();
}

get_allocator

语法:

1
allocator_type get_allocator();

  get_allocator()函数返回链表的配置器。


insert

语法:

1
2
3
iterator insert( iterator pos, const TYPE &val );
void insert( iterator pos, size_type num, const TYPE &val );
void insert( iterator pos, input_iterator start, input_iterator end );

  insert()插入元素val到位置pos,或者插入num个元素val到pos之前,或者插入start到end之间的元素到pos的位置。返回值是一个迭代器,指向被插入的元素。


max_size

语法:

1
size_type max_size();

  max_size()函数返回链表能够储存的元素数目。


merge

语法:

1
2
void merge( list &lst );
void merge( list &lst, Comp compfunction );

  merge()函数把自己和lst链表连接在一起,产生一个整齐排列的组合链表。如果指定compfunction,则将指定函数作为比较的依据。


pop_back

语法:

1
void pop_back();

  pop_back()函数删除链表的最后一个元素。


pop_front

语法:

1
void pop_front();

  pop_front()函数删除链表的第一个元素。


push_back

语法:

1
void push_back( const TYPE &val );

  push_back()将val连接到链表的最后。例如:

1
2
3
list<int> the_list;
for( int i = 0; i < 10; i++ )
the_list.push_back( i );

push_front

语法:

1
void push_front( const TYPE &val );

  push_front()函数将val连接到链表的头部。


rbegin

语法:

1
reverse_iterator rbegin();

  rbegin()函数返回一个逆向迭代器,指向链表的末尾。


remove

语法:

1
void remove( const TYPE &val );

  remove()函数删除链表中所有值为val的元素。例如

1
2
3
4
5
6
7
// 创建一个链表,元素是字母表的前10个元素
list<char> charList;
for( int i=0; i < 10; i++ )
charList.push_front( i + 65 );

// 删除所有'E'的实例
charList.remove( 'E' );

remove_if

语法:

1
void remove_if( UnPred pr );

  remove_if()以一元谓词pr为判断元素的依据,遍历整个链表。如果pr返回true则删除该元素。


rend

语法:

1
reverse_iterator rend();

  rend()函数迭代器指向链表的头部。


resize

语法:

1
void resize( size_type num, TYPE val );

  resize()函数把list的大小改变到num。被加入的多余的元素都被赋值为val。


reverse

语法:

1
void reverse();

  reverse()函数把list所有元素倒转。


size

语法:

1
size_type size();

  size()函数返回list中元素的数量。


sort

语法:

1
2
void sort();
void sort( Comp compfunction );

  sort()函数为链表排序,默认是升序。如果指定compfunction的话,就采用指定函数来判定两个元素的大小。


splice

语法:

1
2
3
void splice( iterator pos, list &lst );
void splice( iterator pos, list &lst, iterator del );
void splice( iterator pos, list &lst, iterator start, iterator end );

  splice()函数把lst连接到pos的位置。如果指定其他参数,则插入lst中del所指元素到现链表的pos上,或者用start和end指定范围。


swap

语法:

1
void swap( list &lst );

  swap()函数交换lst和现链表中的元素。


unique

语法:

1
2
void unique();
void unique( BinPred pr );

  unique()函数删除链表中所有重复的元素。如果指定pr,则使用pr来判定是否删除。

-------- 本文结束 感谢阅读 --------
相关文章
  • STL常用模板:MultiSet多元集合
  • STL常用模板:MultiMap多元字典
  • STL常用模板:Map字典
  • STL常用模板:Bitset位集合
  • STL常用模板:Double Ended Queue双向队列
觉得文章写的不错的话,请我喝瓶怡宝吧!😀
SiriYang 微信支付

微信支付

SiriYang 支付宝

支付宝

  • 本文标题: STL常用模板:List链表
  • 本文作者: SiriYang
  • 创建时间: 2020年01月08日 - 14时01分
  • 修改时间: 2021年10月29日 - 18时10分
  • 本文链接: https://blog.siriyang.cn/posts/20200108141737id.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
C/C++ 文档 STL
STL常用模板:Double Ended Queue双向队列
STL常用模板:Set集合
  • 文章目录
  • 站点概览
SiriYang

SiriYang

努力搬砖攒钱买镜头的摄影迷
320 日志
33 分类
88 标签
RSS
GitHub E-Mail
Creative Commons
Links
  • 友情链接
  • 作品商铺

  1. 概念
  2. 构造函数
  3. 常用函数
    1. assign
    2. back
    3. begin
    4. clear
    5. empty
    6. end
    7. front
    8. get_allocator
    9. insert
    10. max_size
    11. merge
    12. pop_back
    13. pop_front
    14. push_back
    15. push_front
    16. rbegin
    17. remove
    18. remove_if
    19. rend
    20. resize
    21. reverse
    22. size
    23. sort
    24. splice
    25. swap
    26. unique
蜀ICP备19008337号 © 2019 – 2025 SiriYang | 1.7m | 25:41
0%