forward_list学习之操作sort,merge, splice, remove, remove_if, reverse, unique

本篇学习forward_list的操作

sort:对元素进行排序

merge:合并二个已排序列表

splice_after:从另一 forward_list 移动元素

remove:移除满足特定标准的元素

remove_if:移除满足特定标准的元素

reverse:将该链表的所有元素的顺序反转

unique:删除连续的重复元素

代码实现

#include <iostream>
#include <forward_list>

using namespace std;

bool removeCondtion(const int &value)
{
    return (value > 70);
}
void forward_listOperator()
{
    //1.sort:对元素进行排序
    forward_list<int> list1 = {13, 46, 23, 76, 54};
    forward_list<int> list2 = {17, 36, 22, 73, 87};
    cout << " list1的值为:";
    for(auto &val: list1)
        cout << val << "\t";
    cout << endl;
    list1.sort();
    cout << " list1排序后:";
    for(auto &val: list1)
        cout << val << "\t";
    cout << endl;

    cout << " list2的值为:";
    for(auto &val: list2)
        cout << val << "\t";
    cout << endl;
    list2.sort();
    cout << " list2排序后:";
    for(auto &val: list2)
        cout << val << "\t";
    cout << endl;
    //2.merge:合并二个已排序列表
    list1.merge(list2);//合并后list2的内容为空了
    cout << " list1的值为:";
    for(auto &val: list1)
        cout << val << "\t";
    cout << endl;

    //3.splice_after:从另一 forward_list 移动元素
    forward_list<int> list3 = {62, 39, 56};
    auto it1 = list3.begin();//it1指向第一个位置62
    cout << " it1的值为:" << *it1 << endl;
    list2.assign({21, 22, 23});//list2合并后没有值,这里重新赋值
    list3.splice_after(list3.before_begin(), list2);//把list2拼接到list3起始位置,拼接后list2为空
    //现在的it1指向的是62,但位置已是第4个位置了
    cout << " list3的值为:";
    for(auto &val: list3)
        cout << val << "\t";
    cout << endl;

    cout << " list2的值为:";
    for(auto &val: list2)
        cout << val << "\t";
    cout << endl;
    //para1:拼接的位置,para2:拼接的容器,para3:起点,para4终点 范围(start, end)不包括start和end两个元素
    list2.splice_after(list2.before_begin(), list3, list3.begin(), it1);
    cout << " list2的值为:";
    for(auto &val: list2)
        cout << val << "\t";
    cout << endl;
    cout << " list3的值为:";
    for(auto &val: list3)
        cout << val << "\t";
    cout << endl;

    forward_list<int> list4 = { 1, 2, 3 };
    cout << " list4.begin = " << *list4.begin() << endl;
    //把list4里的begin之后的一个值拼接到list3头部元素之前的位置
    list3.splice_after(list3.before_begin(), list4, ++list4.begin());
    cout << " list2的值为:";
    for(auto &val: list2)
        cout << val << "\t";
    cout << endl;
    cout << " list3的值为:";
    for(auto &val: list3)
        cout << val << "\t";
    cout << endl;
    cout << " list4的值为:";
    for(auto &val: list4)
        cout << val << "\t";
    cout << endl;

    //4.remove:移除满足特定标准的元素
    forward_list<int> list5 = {23, 43, 87, 20, 79};
    cout << " list5的值为:";
    for(auto &val: list5)
        cout << val << "\t";
    cout << endl;
    list5.remove(43);
    cout << " list5的值为:";
    for(auto &val: list5)
        cout << val << "\t";
    cout << endl;
    //5.remove_if:移除满足特定标准的元素
    list5.remove_if(removeCondtion);//移除数值大于70的值
    cout << " list5的值为:";
    for(auto &val: list5)
        cout << val << "\t";
    cout << endl;
    //6.reverse:将该链表的所有元素的顺序反转
    forward_list<int> list6 = {10,20,30,40,50,60};
    cout << " list6的值为:";
    for(auto &val: list6)
        cout << val << "\t";
    cout << endl;
    list6.reverse();
    cout << " list6反转后:";
    for(auto &val: list6)
        cout << val << "\t";
    cout << endl;
    //7.unique:删除连续的重复元素
    forward_list<int> list7 = {1, 2, 3, 4,4, 5, 6, 4, 7, 4, 8};
    cout << " list7的值为:";
    for(auto &val: list7)
        cout << val << "\t";
    cout << endl;

    list7.unique();
    cout << " list7删除重复元素后:";
    for(auto &val: list7)
        cout << val << "\t";
    cout << endl;
}

int main()
{
    forward_listOperator();
    cout << endl;

    cout << " Hello World!" << endl;
    return 0;
}

运行结果:

merge合并后,第二个list的内容就为空了。

splice_after拼接后,第二个list里的拼接元素被清空了。

参考:

https://zh.cppreference.com/w/cpp/container/forward_list

http://www.cplusplus.com/reference/forward_list/forward_list/

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值