STL-算法

#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>

using namespace std;

class MyPrint
{
public:
    void operator()(const int &val)
    {
        cout << val << " ";
    }
};

void test01()
{
    vector<int> v1{4, 3, 2, 1, 0};
    //遍历算法
    for_each(v1.begin(), v1.end(), [&](const int &val) {
        cout << val << " ";
    });
    cout << endl;

    sort(v1.begin(), v1.end());
    for_each(v1.begin(), v1.end(), MyPrint());
    cout << endl;
}

class TransForm
{
public:
    int operator()(int val)
    {
        return val;
    }
};

void test02()
{
    vector<int> v1{5, 4, 3, 2, 1, 0};
    vector<int> v2;
    v2.resize(v1.size());
    transform(v1.begin(), v1.end(), v2.begin(), TransForm());
    for_each(v2.begin(), v2.end(), MyPrint());
    cout << endl;
}

void test03()
{
    //find:查找指定元素,找到返回指定元素的迭代器
    vector<int> v1{0, 1, 2, 3, 4};
    vector<int>::iterator it = find(v1.begin(), v1.end(), 2);
    if (it == v1.end())
    {
        cout << "没找到!" << endl;
    }
    else
    {
        cout << "找到:" << *it << endl;
    }

    //find_if:按条件查找元素
    it = find_if(v1.begin(), v1.end(), [&](int &val) -> bool {
        return val > 2;
    });
    if (it == v1.end())
    {
        cout << "没找到大于2的数!" << endl;
    }
    else
    {
        cout << "找到第一个大于2的数:" << *it << endl;
    }

    //adjacent_find:查找相邻重复元素
    vector<char> v2{'A', 'B', 'C', 'D', 'D', 'E'};
    auto it2 = adjacent_find(v2.begin(), v2.end());
    if (it2 == v2.end())
    {
        cout << "没有重复的元素!" << endl;
    }
    else
    {
        cout << "第一个重复的元素是:" << *it2 << endl;
    }

    //binary_search:查找指定元素是否存在,查到返回true,否则false
    //在无序序列中不可用
    vector<int> v3{2, 0, 2, 3, 1};
    sort(v3.begin(), v3.end()); //在无序序列中不可用
    bool ret = binary_search(v3.begin(), v3.end(), 2);
    if (ret)
    {
        int num = count(v3.begin(), v3.end(), 2);
        cout << "有" << num << "个元素2!" << endl;
    }
    else
    {
        cout << "没有元素2!" << endl;
    }

    //count_if:按条件统计元素出现次数
    int num = count_if(v3.begin(), v3.end(), [&](int &val) -> bool {
        return val > 2;
    });
    cout << "大于2的个数:" << num << endl;
}

void test04()
{
    //sort:对容器内元素进行排序
    vector<int> v1{2, 5, 3, 0, 1};
    sort(v1.begin(), v1.end());
    for_each(v1.begin(), v1.end(), MyPrint());
    cout << endl;
    sort(v1.begin(), v1.end(), greater<int>());
    for_each(v1.begin(), v1.end(), MyPrint());
    cout << endl;

    //random_shuffle:指定范围内的元素随机调整次序
    //随机数种子
    srand(static_cast<unsigned int>(time(nullptr)));
    random_shuffle(v1.begin(), v1.end());
    for_each(v1.begin(), v1.end(), MyPrint());
    cout << endl;

    //merge:容器元素合并,并存储到另一容器中
    //两个容器必须是有序的!
    vector<int> v2{8, 10, 7, 9};
    sort(v2.begin(), v2.end(), greater<int>());
    sort(v1.begin(), v1.end(), greater<int>());
    vector<int> vTarget;
    vTarget.resize(v1.size() + v2.size());
    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin(), greater<int>());
    for_each(vTarget.begin(), vTarget.end(), MyPrint());
    cout << endl;

    //reverse:将容器内元素进行反转
    reverse(vTarget.begin(), vTarget.end());
    for_each(vTarget.begin(), vTarget.end(), MyPrint());
    cout << endl;
}

void test05()
{
    //copy:容器内指定范围的元素拷贝到另一容器中
    vector<int> v1{0, 1, 2, 3, 4};
    vector<int> v2;
    v2.resize(v1.size());
    copy(v1.begin(), v1.end(), v2.begin());
    for_each(v2.begin(), v2.end(), MyPrint());
    cout << endl;

    //replace:将容器内指定范围内的旧元素修改为新元素
    replace(v1.begin(), v1.end(), 1, 10); //将容器中1替换为10
    for_each(v1.begin(), v1.end(), MyPrint());
    cout << endl;

    //replace_if:将区间内满足条件的元素,替换成指定元素
    replace_if(v1.begin(), v1.end(), [&](int &val) {
        return val < 3;
    }, 0);
    for_each(v1.begin(), v1.end(), MyPrint());
    cout << endl;

    //swap:互换两个容器的元素
    swap(v1, v2);
    for_each(v1.begin(), v1.end(), MyPrint());
    cout << endl;
}

void test06()
{
    //accumulate:计算区间内元素累计之和
    vector<int> v1{0, 1, 2, 3, 4};
    int sum = accumulate(v1.begin(), v1.end(), 0);
    cout << "sum = " << sum << endl;

    //fill:向容器中填充指定的元素
    vector<char> v2;
    v2.resize(10);
    fill(v2.begin(), v2.end(), 'X');
    for_each(v2.begin(), v2.end(), [&](char &val) {
        cout << val << " ";
    });
    cout << endl;
}

void test07()
{
    //set_intersection:求两个容器的交集
    //两个集合必须是有序序列
    vector<int> v1{0, 1, 2, 3, 4};
    vector<int> v2{3, 4, 5, 6, 7};
    vector<int> v3;
    v3.resize(min(v1.size(), v2.size()));
    //返回目标容器的最后一个元素的迭代器地址
    vector<int>::iterator it1 = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
    //for_each(v5.begin(), it1, MyPrint());
    for_each(v3.begin(), v3.end(), MyPrint());
    cout << endl;

    //set_union:求两个集合的并集
    //两个集合必须是有序序列
    vector<int> v4;
    v4.resize(v1.size() + v2.size());
    //返回目标容器的最后一个元素的迭代器地址
    auto it2 = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v4.begin());
    //for_each(v5.begin(), it2, MyPrint());
    for_each(v4.begin(), v4.end(), MyPrint());
    cout << endl;

    //set_difference:求两个集合的差集
    //两个集合必须是有序序列
    vector<int> v5;
    //取两个里面较大的值给目标容器开辟空间
    v5.resize(max(v1.size(), v2.size()));
    //返回目标容器的最后一个元素的迭代器地址
    auto it3 = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v5.begin());
    //for_each(v5.begin(), it3, MyPrint());
    for_each(v5.begin(), v5.end(), MyPrint());
    cout << endl;
}

int main(int argc, char const *argv[])
{
    //for_each基本用法:遍历
    test01();

    //transform:搬运容器到另一个容器中。
    //搬运的目标容器比需要提前开辟空间,否则无法正常搬运
    test02();

    //常用的查找算法:find, find_if, adjacent_find, binary_search, count, count_if
    test03();

    //常用的排序算法:sort, random_shuffle, merge, reverse
    test04();

    //常用的拷贝和替换算法:copy, replace, replace_if, swap
    test05();

    //常用的算术生成算法:accumulate, fill
    test06();

    //常用的集合算法:set_intersection, set_union, set_difference
    test07();

    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值