STL list合并

#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
#include<list>
#include<ctime>
using namespace std;
struct Node
    {
        int a;
        char c;
        //头增加ListCoutAdd()
        Node(int d, int e)
        {
            a = d;
            c = e;
        }
        //删除ListDeleteChange(),构成重载。ListDo()find查找重载。
         bool operator ==(const Node& f)const
        {
            if (f.a == this->a && f.c == this->c)
            {
                return true;
            }
            return false;
        }
         //ListDo(),构成重载。
         bool operator <(const Node& f)const//sort重载默认小于号。
         {
             if (f.a < this->a  &&f.c <= this->c)//判定条件。从大到小。
             {
                 return true;
             }
             return false;
         }
    };
void fun(Node& d)
    {
        cout << d.a << " " << d.c << endl;
    }

void ListDo()
{
        list<Node> db;
        db.push_front(Node(15, 'b'));
        db.push_front(Node(14, 'b'));
        db.push_front(Node(12, 'b'));
        db.push_front(Node(11, 'b'));
        db.push_front(Node(13, 'b'));
        db.push_back(Node(16, 'b'));
        db.sort();

        list <Node> db1;
        db1.assign(10,Node(123, 'a'));// 赋值

        //交换数值
        //db1.swap(db);//db与db1交换数据。

        //翻转数据
        //db.reverse();

        //排序
        //db.sort();//成员函数排序。默认从大到小,涉及到结构体(其中的数据类型如char类型的比较)需要重载。
         
        //合并两个列表
        //db.merge(db1);//(两个列表必须是有序的,否则会崩溃,崩溃可以改结构体中重载函数中if的判定大于或小于)。
        
        //查找。
        list<Node>::iterator  ite=     find(db.begin(), db.end(), Node(12, 'b'));//未找到会崩溃的,在一个容器中寻找一个成员,返回这个成员的迭代器。
        cout << ite->a << " "<<ite->c<<endl;
        //for_each(db.begin(), db.end(), fun);

}
int main()
{
    ListDo();
    system("pause");
    return 0;
}
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

clown_30

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值