• 给你两个集合,要求{A} + {B}


    先看题:
    在这里插入图片描述
    看完题后你会觉得:哇,好简单,STL一下就出来啦。

    #include 
    #include 
    
    using namespace std;
    
    int main() {
        int n, m;
        while (cin >> n >> m) {
            set<int> set_a;
    
            for (int i = 0; i < n+m; i++) {
                int element;
                cin >> element;
                set_a.insert(element);
            }
    
            for (const auto& element : set_a) {
                cout << element << " ";
            }
            cout << endl;
        }
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    可能会有一点点的变式,也就是把 set set_a;放循环外面,循环中加一个set_a.clear();
    如果你不想使用循环遍历容器也可以使用迭代器遍历,
    当然这段代码有一点点小问题,最后一个数的后面应该不含空格
    例下:来自这里

    #include 
    #include 
    using namespace std;
     
    int main()
    {
        int n, m, val;
        set<int> result;
     
        while(cin >> n >> m) {
            result.clear();
     
            for(int i=1; i<=n+m; i++) {
                cin >> val;
                result.insert(val);
            }
     
            bool nofirstflag = false;
            for(set<int>::iterator it = result.begin(); it != result.end(); it++) {
                if(nofirstflag)
                    cout << " ";
                nofirstflag = true;
     
                cout << *it;
            }
            cout << endl;
        }
     
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    当然也有可能用的数组:

    #include 
    #include 
    #include 
    
    using namespace std;
    
    int main()
    {
        int n, m;
        while(cin>>n>>m)
        {
            int temp;
            vector <int> vector_a;
            for (int i = 0; i < n+m; i++)
            {
                cin >> temp;
                vector_a.push_back(temp);
            }
            sort(vector_a.begin(), vector_a.end());
            cout << vector_a[0];
            for (int i = 1; i < n+m; i++)
                if (vector_a[i] != vector_a[i-1])
                    cout << " " << vector_a[i];
            cout<<endl;
        }
        
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    常规的看完了,让我们看看“非常规“的:

    #include 
    #include 
    
    using namespace std;
    
    int main() {
        int n, m;
        while (cin >> n >> m) {
            set<int> set_a;
            set<int> set_b;
    
            for (int i = 0; i < n; i++) {
                int element;
                cin >> element;
                set_a.insert(element);
            }
    
            for (int i = 0; i < m; i++) {
                int element;
                cin >> element;
                set_b.insert(element);
            }
    
            set<int> union_set;
            for (const auto& element : set_a) {
                union_set.insert(element);
            }
            for (const auto& element : set_b) {
                union_set.insert(element);
            }
    
            for (const auto& element : union_set) {
                cout << element << " ";
            }
            cout << endl;
        }
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    这是我第一次通过的,可以看到几乎没有用到一点set的用处,多了两个for循环,在逻辑上多占了一倍的空间,多耗费了一倍的时间。当我们学会了STL的时候,不仅可以解决上述问题,更重要的是,繁杂重复的代码会将你的思路打断,让你感觉敲代码像是搬砖,并且代码一多会使代码的可读性大打折扣,这可不好,你总不能说,这段代码真差,结果一查,是你一个月前写的。

  • 相关阅读:
    【Linux前篇 】VMWare虚拟机安装与环境配置及远程连接 —— windows版
    树的递归算法与非递归(迭代)的转化重点理解代码(上篇)
    【数字信号处理】序列傅里叶变换(FT)的物理意义
    Kubernetes-----介绍
    linux批量修改文件名及文件内容
    【k8s】Kubernetes版本v1.17.3 kubesphere 3.1.1 默认用户登录失败
    java毕业设计天狗电子商城系统mybatis+源码+调试部署+系统+数据库+lw
    Fluent的msh格式网格学习
    anaconda安装及配置+pytorch安装与配置(自用笔记)
    java自定义异常
  • 原文地址:https://blog.csdn.net/m0_64799907/article/details/133319892