C++笔记打卡第23天(STL常用算法)

1.常用排序算法

  • sort:对容器内元素进行排序

    cpp 复制代码
    class Myprint
    {
    public:
        void operator()(int val)
        {
            cout << val << " ";
        }
    };
    
    // 查自定义数据类型
    void test01()
    {
        vector<int> v;
        v.push_back(10);
        v.push_back(20);
        v.push_back(15);
        v.push_back(25);
        v.push_back(0);
        // 利用sort算法升序
        sort(v.begin(), v.end());
        for_each(v.begin(), v.end(), Myprint());
        cout << endl;
    
        // 降序
        sort(v.begin(), v.end(), greater<int>());
        for_each(v.begin(), v.end(), Myprint());
        cout << endl;
    }
  • random_shuffle:随机调整顺序

    cpp 复制代码
    #include<algorithm>
    
    void test01()
    {
        srand((unsigned int)time(NULL));
        vector<int> v;
        for(int i=0; i<10; i++)
        {
            v.push_back(i);
        }
        random_shuffle(v.begin(), v.end());
        for_each(v.begin(), v.end(), Myprint());
        cout << endl;
    }
  • merge:有序的两个容器元素合并,并存储到另一个容器中,输出仍然是有序的

    cpp 复制代码
    void test01()
    {
        vector<int> v1;
        vector<int> v2;
        for(int i=0; i<10; i++)
        {
            v1.push_back(i);
            v2.push_back(i+1);
        }
        vector<int> v3;
        v3.resize(v1.size() + v2.size());
        merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
        // 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
    }
  • reverse:反转指定范围的元素

    cpp 复制代码
    void test01()
    {
        vector<int> v1;
        for(int i=0; i<10; i++)
        {
            v1.push_back(i);
        }
    
        reverse(v1.begin(), v1.end());  // 9 8 7 6 5 4 3 2 1 0
    }

2.常用拷贝和替换算法

  • copy:容器内指定范围的元素拷贝到另一容器中

    cpp 复制代码
    void test01()
    {
        vector<int> v1;
        for(int i=0; i<10; i++)
        {
            v1.push_back(i);
        }
    
        vector<int> v2;
        v2.resize(v1.size());
        copy(v1.begin(), v1.end(), v2.begin()); // 0 1 2 3 4 5 6 7 8 9
    }
  • replace:将容器内指定范围的旧元素修改为新元素

    cpp 复制代码
    void test01()
    {
        vector<int> v1;
        for(int i=0; i<10; i++)
        {
            v1.push_back(i);
        }
        // 将容器内所有的1替换为11
        replace(v1.begin(), v1.end(), 1, 11);
    }
  • replace_if:容器内指定范围满足条件的元素替换为新元素

    cpp 复制代码
    class Myprint
    {
    public:
        void operator()(int val)
        {
            cout << val << " ";
        }
    };
    
    class MyCompare
    {
    public:
        bool operator()(int val)
        {
            return val > 7;
        }
    };
    
    void test01()
    {
        vector<int> v1;
        for(int i=0; i<10; i++)
        {
            v1.push_back(i);
        }
        // 将大于等于7的替换为100
        replace_if(v1.begin(), v1.end(), MyCompare(), 100);
    }
  • swap:交换两个同种类型的容器内的元素

    cpp 复制代码
    void test01()
    {
        vector<int> v1;
        vector<int> v2;
        for(int i=0; i<10; i++)
        {
            v1.push_back(i);
            v2.push_back(i+100);
        }
    
        swap(v1, v2);
    }

3.常用算术生成算法

  • 算术生成算法属于小型算法,使用时包含的头文件为 #include<numeric>

  • accumulate:计算容器元素累计总和

    cpp 复制代码
    #include<numeric>
    
    void test01()
    {
        vector<int> v1;
        for(int i=0; i<=100; i++)
        {
            v1.push_back(i);
        }
        // 从0开始将所有元素加起来
        int total = accumulate(v1.begin(), v1.end(), 0);
        cout << total << endl;
    }
  • fill:向容器中添加元素

    cpp 复制代码
    void test01()
    {
        vector<int> v1;
        v1.resize(10); // 10个0
        // 后期重新填充, 将范围内的值全改为100
        fill(v1.begin(), v1.end(), 100);
        for_each(v1.begin(),v1.end(), Myprint());
        cout << endl;
    }

4.常用集合算法

  • set_intersection:求两个容器的交集

    cpp 复制代码
    void test01()
    {
        vector<int> v1;
        vector<int> v2;
        for(int i=0; i<10; i++)
        {
            v1.push_back(i);
            v2.push_back(i+2);
        }
        vector<int> v3;
        // 极限情况:一个容器是另一个容器的子集
        v3.resize(min(v1.size(), v2.size()));
        // 获取交集,返回末尾的迭代器
        vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
        for_each(v3.begin(), itEnd, Myprint());
        cout << endl;
    }
  • set_union:求两个容器的并集**(两个集合必须是有序的序列)**

    cpp 复制代码
    void test01()
    {
        vector<int> v1;
        vector<int> v2;
        for(int i=0; i<10; i++)
        {
            v1.push_back(i);
            v2.push_back(i+2);
        }
        vector<int> v3;
        // 极限情况:两个容器没有交集
        v3.resize(v1.size()+v2.size());
        // 获取交集,返回并集中最后一个元素的位置
        vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
        for_each(v3.begin(), itEnd, Myprint());
        cout << endl;
    }
  • set_difference:求两个容器的差集

    cpp 复制代码
    void test01()
    {
        vector<int> v1;
        vector<int> v2;
        for(int i=0; i<10; i++)
        {
            v1.push_back(i);
            v2.push_back(i+5);
        }
        vector<int> v3;
        v3.resize(max(v1.size(),v2.size()));
        cout << "v1和v2的差集为:" << endl;
        vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
        for_each(v3.begin(), itEnd, Myprint());
        cout << endl;
        
        cout << "v2和v1的差集为:" << endl;
        vector<int>::iterator itEnd2 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v3.begin());
        for_each(v3.begin(), itEnd2, Myprint());
        cout << endl;
    }
相关推荐
一只码代码的章鱼5 分钟前
数据结构与算法-图论-最短路-单源最短路的建图方式
算法·图论
黄金龙PLUS14 分钟前
十分简单的流密码算法RC4
算法·网络安全·密码学·哈希算法·同态加密
StickToForever18 分钟前
第5章 软件工程(二)
经验分享·笔记·学习·职场和发展
我想吃余19 分钟前
【初探数据结构】时间复杂度和空间复杂度
数据结构·算法
a_j5819 分钟前
算法与数据结构(环形链表II)
数据结构·算法·链表
StickToForever25 分钟前
第5章 软件工程(一)
经验分享·笔记·学习·职场和发展
攻城狮7号32 分钟前
【第五节】C++设计模式(创建型模式)-Prototype(原型)模式
c++·设计模式·原型模式
请卧龙先生出山34 分钟前
c++day4
开发语言·c++
愚戏师38 分钟前
从零到一学习c++(基础篇--筑基期十一-类)
开发语言·数据结构·c++·学习·算法
Magnetic_h1 小时前
《Effective Objective-C》阅读笔记(下)
笔记·ios·objective-c