STL学习记录(七):Forward List

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)交换list1list2 数据
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之后插入nelem的拷贝并返回第一个拷贝的位置(当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中的所有元素逆序

需要注意的是迭代器中的before_begin( )和cbefore_begin( ),返回的迭代器位置是无效的,它所指元素是虚出来的,相当于数据结构课程中单链表虚构头节点。这样做是为了方便一些操作。若在算法中使用了该迭代器这会导致运行时错误,若引用该位置则导致未定义行为。

一些代码示例:

//特定位置插入元素方法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

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://my.oschina.net/yourfirst/blog/513780

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值