STL学习笔记6— —容器forward_list
来源:互联网 发布:软件过程成熟度cmm 编辑:程序博客网 时间:2024/06/08 09:43
简介
在头文件 <forward_forward_list> 中定义
namespace std { template <typename T, typename Allocator = allocator<T> >class forward_forward_list;}
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
单向列表是一个容器,支持在其任何地方快速插入和删除元素,不支持快速的随机访问。它被实现为单向链表,和C中它的实现相比,基本上不会有任何开销。
当不需要双向迭代的时候,与forward_list相比,该容器具有更高的空间利用率。
插入和删除操作不会使指向其他元素指针、引用和迭代器失效。
forward_list操作
构造、复制与析构
forward_list<Elem> c //默认构造函数;创建一个空forward_listforward_list<Elem> c(c2) //复制构造函数;创建一个新的forward_list作为c2的副本(所有元素都被复制)forward_list<Elem> c = c2 //复制构造函数;创建一个新的forward_list作为c2的副本(所有元素都被复制)forward_list<Elem> c(rv) //移动构造函数;使用右值对象rv创建一个新forward_listforward_list<Elem> c = rv //移动构造函数;使用右值对象rv创建一个新forward_listforward_list<Elem> c(n) //使用默认构造函数创建含有n个元素的forward_listforward_list<Elem> c(n,elem) //创建一个forward_list,并使用n个elem进行初始化forward_list<Elem> c(beg,end) //创建一个forward_list,并使用beg到end范围内的值进行初始化forward_list<Elem> c(initlist) //创建一个forward_list,并使用初始化列表进行初始化forward_list<Elem> c = initlist //创建一个forward_list,并使用初始化列表进行初始化c.~forward_list() //销毁所有元素并释放内存
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
非变动性操作
c.empty() //判断容器是否为空c.max_size() //返回可容纳的元素最大数量c1 == c2 //判断c1与c2是否相等c1 != c2 //判断c1与c2是否不相等,等同于!(c1==c2)c1 < c2 //判断c1是否小于c2c1 > c2 //判断c1是否大于c2c1 <= c2 //判断c1是否小于等于c2c1 >= c2 //判断c1是否大于等于c2
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
赋值
c = c2 //将c2所有元素赋值给cc = rv //将右值对象rv的所有元素移动赋值给cc = initlist //使用初始化列表进行赋值c.assign(initlist) //使用初始化列表进行赋值c.assign(n,elem) //使用n个elem元素进行赋值c.assign(beg,end) //使用beg到end范围内的元素进行赋值c1.swap(c2) //交换c1和c2的数swap(c1,c2) //交换c1和c2的数
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
元素存取
c.front() //返回第一个元素,不检查第一个元素是否存在
- 1
- 1
迭代器相关函数
c.begin() //返回一个双向迭代器,指向第一个元素c.end() //返回一个双向迭代器,指向最后一个元素c.cbegin() //返回一个双向常迭代器,指向第一个元素c.cend() //返回一个双向常迭代器,指向最后一个元素c.before_begin() //返回一个前向迭代器,指向第一个元素之前的位置c.cbefore_begin() //返回一个前向常迭代器,指向第一个元素之前的位置
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
插入和移除元素
c.push_front(elem) //在头部添加一个elem副本c.pop_front() //移除头部元素(但不回传)c.insert_after(pos,elem) //在迭代器位置之后插入一个elem副本,并返回新元素的位置c.insert_after(pos,n,elem) //在迭代器位置之后插入n个elem副本,并返回第一个新元素的位置;若无新插入值,返回原位置c.insert_after(pos,beg,end) //在迭代器位置之后插入范围beg到end的所有元素的副本,并返回第一个新元素的位置;若无新插入值,返回原位置c.insert_after(pos,initforward_list) //在迭代器位置之后插入初始化列表的所有元素的副本,并返回第一个新元素的位置;若无新插入值,返回原位置c.emplace_after(pos,args...) //在迭代器位置之后插入一个使用args初始化的元素副本,并返回新元素的位置c.emplace_front(args...) //在头部添加一个使用args初始化的元素副本,无返回值c.erase_after(pos) //移除迭代器位置的元素,无返回值c.erase_after(beg,end) //移除beg到end范围内的所有元素,无返回值c.remove(val) //移除所有值为val的元素c.remove_if(op) //移除所有满足op条件的元素c.resize(num) //将元素数量设为num(如果size()增大,多出来的元素使用默认构造函数创建)c.resize(num,elem) //将元素数量设为num(如果size()增大,多出来的元素都是elem的副本)c.clear() //移除所以元素,清空容器
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
特殊修改操作
c.unique() //若存在相邻而数值相等的元素,移除重复元素c.unique(op) //若存在相邻而数值相等的元素,且满足op条件时,移除重复元素c.splice_after(pos,c2) //将c2内的所有元素转移到c1内pos所指的位置之后c.splice_after(pos,c2,c2pos) //将c2内c2pos之后的元素转移到c1内pos所指的位置之后c.splice_after(pos,c2,c2beg,c2end) //将c2内从c2beg到c2end区间内的所有元素转移到c1内pos所指的位置之后c.sort() //以operator<为准则,对所有元素排序c.sort(op) //以op为准则,对所有元素排序c.merge(c2) //假设c1和c2都包含已序元素,将c2的全部元素转移到c1.并保证合并后的forward_list仍为已序c.merge(c2,op) //假设c1和c2都包含op原则下已序元素,将c2的全部元素转移到c1.并保证合并后的forward_list在op原则下仍为已序c.reverse() //将所有元素反序
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
栗子
#include <forward_list>#include <iostream>#include <algorithm>#include <iterator>#include <string>using namespace std;void printLists (const string& s, const forward_list<int>& l1, const forward_list<int>& l2){ cout << s << endl; cout << " list1: "; copy (l1.cbegin(), l1.cend(), ostream_iterator<int>(cout," ")); cout << endl << " list2: "; copy (l2.cbegin(), l2.cend(), ostream_iterator<int>(cout," ")); cout << endl;}int main(){ //创建两个前向列表 forward_list<int> list1 = { 1, 2, 3, 4 }; forward_list<int> list2 = { 77, 88, 99 }; printLists ("initial:", list1, list2); //在list2头部插入6个新元素 list2.insert_after(list2.before_begin(),99); list2.push_front(10); list2.insert_after(list2.before_begin(), {10,11,12,13} ); printLists ("6 new elems:", list1, list2); //在list1头部插入list2全部元素 list1.insert_after(list1.before_begin(), list2.begin(),list2.end()); printLists ("list2 into list1:", list1, list2); //删除list2的第二个元素,删除list2中值为99的元素后面的所有元素 list2.erase_after(list2.begin()); list2.erase_after(find(list2.begin(),list2.end(), 99), list2.end()); printLists ("delete 2nd and after 99:", list1, list2); //对list1排序,并把list1赋值给list2,对list2去重 list1.sort(); list2 = list1; list2.unique(); printLists ("sorted and unique:", list1, list2); //把已序的list2合并到list1中 list1.merge(list2); printLists ("merged:", list1, list2); return 0;}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
输出
initial:list1: 1 2 3 4list2: 77 88 996 new elems:list1: 1 2 3 4list2: 10 11 12 13 10 99 77 88 99list2 into list1:list1: 10 11 12 13 10 99 77 88 99 1 2 3 4list2: 10 11 12 13 10 99 77 88 99delete 2nd and after 99:list1: 10 11 12 13 10 99 77 88 99 1 2 3 4list2: 10 12 13 10 99sorted and unique:list1: 1 2 3 4 10 10 11 12 13 77 88 99 99list2: 1 2 3 4 10 11 12 13 77 88 99merged:list1: 1 1 2 2 3 3 4 4 10 10 10 11 11 12 12 13 13 77 77 88 88 99 99 99list2:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
阅读全文
0 0
- STL学习笔记— —容器forward_list
- STL学习笔记6— —容器forward_list
- STL容器forward_list
- C++ STL 学习笔记 list,forward_list
- STL学习笔记——容器
- STL学习笔记——map容器
- STL学习笔记— —容器array
- STL学习笔记— —容器vector
- STL学习笔记— —容器deque
- STL学习笔记— —容器list
- STL学习笔记-容器
- STL forward_list
- stl::forward_list
- STL学习----入门(1)[forward_list]
- STL学习笔记——序列式容器list
- STL学习笔记——序列式容器deque
- STL学习笔记——02.容器类
- STL学习笔记— —容器set和multiset
- 解决Ubuntu/LInux QT5.8(QTCreater4.1)不能输入中文问题
- Java克隆(Clone)的应用
- python机器学习(2:KNN算法)
- 直通BAT--数据结构与算法六(二分搜索)
- 四种常见的 POST 提交数据方式
- STL学习笔记6— —容器forward_list
- 【DL--03】深度学习基本概念—张量
- android
- 值类型和引用类型
- Myeclipse2013下安装Axis2插件
- PAT甲级 1028
- 【F2C】elasitcsearch/ELK学习随笔——X-PACK身份验证
- 考研英语笔记——时文长难句二
- 使用jquery提交form表单-submit方法总结