• STL list合并


    #include
    #include
    #include
    #include
    #include
    #include
    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 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 db1;
            db1.assign(10,Node(123, 'a'));// 赋值

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

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

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

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

  • 相关阅读:
    《C++ Primer》第5章 语句
    008.分隔符、循环、比较
    STM32单片机OLED贪吃蛇游戏记分计时
    指针的进阶
    2023/10/5 下午3:38:53 SCROLLINFO scrollInfo;
    CPP代码检查工具
    Mysql面试(事务)
    开源项目观察8月报
    Unity3D开发流程及注意事项
    LeetCode 697. Degree of an Array
  • 原文地址:https://blog.csdn.net/qq_52119661/article/details/125885923