STL学习记录(七):Forward List
2015-10-06 00:00
225 查看
STL顺序容器Forward List
Forward List简介
Forward List容器在c++11标准之前是没有的,Forward List形如数据结构中的单链表结构,所有它的很多特点也由此决定。比如,在任意位置的插入与删除速度都很快,不支持元素的随机访问。与List容器相比因为是链表是单向的,所有在存储方面Forward List的效率更高。Forward List的常见操作如下:迭代器
操作 | 说明 |
---|---|
list.begin( ) | 返回list的第一个元素的单向迭代器 |
list.end( ) | 返回list的最后一个元素的单向迭代器 |
list.cbegin( ) | 返回list的第一个元素的const型单向迭代器 |
list.cend( ) | 返回list的最后一个元素的const型单向迭代器 |
list.before_begin( ) | 返回list第一个元素之前位置的单向迭代器 |
list.cbefore_begin( ) | 返回list第一个元素之前位置的const型单向迭代器 |
容量
操作 | 说明 |
---|---|
list.empty( ) | 判断list是否为空 |
list.max_size( ) | 返回list的最大的容量 |
直接访问
操作 | 说明 |
---|---|
list.front( ) | 返回第一个元素(不检查该元素是否存在) |
赋值类
操作 | 说明 |
---|---|
swap(list1,list2) | 交换list1与list2 数据 |
list1.swap(list2) | 交换list1与list中的所有数据 |
list.assign(n,elem) | 将n个元素elem的拷贝赋值给list |
list.assign(beg,end) | 将[beg,end)范围内的元素赋值给list |
list.assign({*x1,x2,……}) | 将初始化列表中的所有元素都赋值给list |
修改类
操作 | 说明 |
---|---|
list.clear( ) | 删除list中的所有元素 |
list.erase_after(pos) | 删除位于pos的元素 |
list.erase_after(beg,end) | 删除位于[beg,end)之间的元素 |
list.resize(num) | 重置list的大小,若list变大则新增元素使用该元素默认构造函数构造 |
list.resize(num,elem) | 重置list的大小,若list变大则新增元素使用elem 的拷贝 |
list.remove(val) | 移除所有值为val的元素 |
list.remove_if(op) | 移除listh中元素经过op后为true的所有元素 |
list.push_front(elem) | 将elem加到list的前面 |
list.pop_front( ) | 移除list中的第一个元素(不是返回该值) |
list.insert_after(pos,elem) | 在pos之后插入elem的拷贝并返回该元素的位置 |
list,insert_after(pos,n,elem) | 在pos之后插入n个elem的拷贝并返回第一个拷贝的位置(当n==0返回pos |
list.insert_after(pos,beg,end) | 将[beg,end)之间的元素插入到pos之后(当beg==end返回pos |
list.emplace_after(pos,args…) | 将用参数args…初始化的元素插入到pos之后并返回该元素的位置c++11 |
list.emplace_front(args…) | 将用参数args…初始化的元素添加到list的开头(不返回任何值)c++11 |
list.insert_after(pos,{*x1,x2,…}* | 将列表{*x1,x2,…}中所有元素的拷贝插入到*pos之后并返回第一个拷贝的位置(当为空时返回pos)c++11 |
特有的修改操作
操作 | 说明 |
---|---|
list.unique( ) | 移除所有连续的具有相同值的重复元素 |
list.unique(op | 当元素的op操作为真时,移除所有连续的具有相同值的重复元素 |
list1.splice_after(pos,list2) | 将list2中的所有元素移到list1的迭代器pos之后 |
list1.splice_after(pos,list2,pos2) | 将list2迭代器Pos2所指元素移到list1迭代器pos之后 |
list1.splice_after(pos,list2,beg2,end2) | list2中位与[beg2,end2)的所有元素移到list的pos之后 |
list.sort( ) | 对所有元素进行排序采用符号 < |
list.sort(op) | 对所有元素进行排序采用符号 op |
list1.merge(list2,op) | 该操作必须保证list1与list2操作前有序,将list2中的所有元素移到list1中并保证依然有序 |
list1.merge(list2,op) | 该操作必须保证list1与list2操作前有序,将list2中的所有元素移到list1中并保证依然有序,顺序由op操作符确定 |
list.reverse( ) | 将list中的所有元素逆序 |
一些代码示例:
//特定位置插入元素方法1 #include<forward_list> #include<iostream> using namespace std; int main() { forward_list<int> list = {1,2,4,5,6}; auto pre = list.before_begin(); for(auto pos=list.begin();pos!=list.end();++pos,++pre) { if(*pos%4==0) break; } list.insert_after(pre,3); for(auto pos=list.begin();pos!=list.end();++pos) cout<<*pos<<" "; cout<<endl; return 0; } //方法2 #include<forward_list> #include<iterator> #include<iostream> using namespace std; int main() { forward_list<int> list = {1,2,4,5,6}; auto pre = list.before_begin(); for(;next(pre)!=list.end();++pre) { if(*next(pre)%4==0) break; }//next操作是从c++11开始为迭代器类型的自有,具体在后面介绍 list.insert_after(pre,3); for(auto pos=list.begin();pos!=list.end();++pos) cout<<*pos<<" "; cout<<endl; return 0; }
程序的输出均为:
1 2 3 4 5 6
#include<forward_list> #include<iostream> using namespace std; int main() { forward_list<int> list1={1,2,3,12,4,5}; forward_list<int> list2={10,11,13,14,15}; auto pre_12 = list1.before_begin(); for(auto pos_12=list1.begin();pos_12!=list1.end();++pos_12,++pre_12) { if(*pos_12==12) break; } auto pre_13 = list2.before_begin(); for(auto pos_13=list2.begin();pos_13!=list2.end();++pos_13,++pre_13) { if(*pos_13==13) break; } list1.splice_after(pre_13,list2,pre_12); cout<<"list1: "; for(auto pos=list1.begin();pos!=list1.end();++pos) cout<<*pos<<" "; cout<<endl; cout<<"list: "; for(auto pos=list2.begin();pos!=list2.end();++pos) cout<<*pos<<" "; cout<<endl; return 0; }
程序输出为:
list: 1 2 3 4 5
list: 10 11 12 13 14 15
版权声明:本文为博主原创文章,未经博主允许不得转载。
相关文章推荐
- C#中Ilist与list的区别小结
- C#中IList<T>与List<T>的区别深入解析
- C#对list列表进行随机排序的方法
- 浅析STL中的常用算法
- STL区间成员函数及区间算法总结
- Vc++ 控件List Control用法总结
- C++实现的泛型List类分享
- C#使用IComparer自定义List类实现排序的方法
- C# Datagridview绑定List方法代码
- javascript radio list的实现细节(多浏览器兼容)
- c++ STL容器总结之:vertor与list的应用
- C++在成员函数中使用STL的find_if函数实例
- 大家注意vector, list, set, map成员函数erase
- ASP.NET―001:GridView绑定List、页面返回值具体实现
- List all the Databases on a SQL Server
- js Map List 遍历使用示例
- Java比较两个List的值是否相等的方法
- Java List双击事件实现方法
- velocity显示List与Map的方法详细解析
- Python3 列表