C++ all_of、any_of及none_of排序算法详解(深入了解,一文学会)

algorithm 头文件中定义了 3 种算法,用来检查在算法应用到序列中的元素上时,什么时候使谓词返回 true。这些算法的前两个参数是定义谓词应用范围的输入迭代器;第三个参数指定了谓词。检查元素是否能让谓词返回 true 似乎很简单,但它却是十分有用的。

例如,可以检查所有学生是否通过了考试,或者检查所有学生是否都参加了课程,或者检查有没有眼睛发绿的 Person 对象,甚至可以检查每个 Dog 对象是否度过了它自己的一天。谓词可以简单,也可以复杂,这取决于你。检查元素属性的三种算法是:

  • all_of() 算法会返回 true,前提是序列中的所有元素都可以使谓词返回 true。
  • any_of() 算法会返回 true,前提是序列中的任意一个元素都可以使谓词返回 true。
  • none_of() 算法会返回 true,前提是序列中没有元素可以使谓词返回 true。

本文作者原创,转载请附上文章出处与本文链接。

C++ all_of、any_of及none_of排序算法详解(深入了解,一文学会)目录

1 all_of()算法

2 any_of() 算法

3 none_of() 算法

4 count count_if 范围查找


1 all_of()算法

        传入一个数组 good_age 来检测 ages 数组内是否存在比 good_age数组大的值

#include <iostream>
#include < algorithm >
#include <vector>
int main()
{
    std::vector<int> ages{ 22, 19, 46, 75, 54, 19, 27, 66, 61, 33, 22, 19 };

    int good_age{ 100 };
    std::cout << (std::all_of(std::begin(ages), std::end(ages), [good_age](int age) { return age < good_age; }) ? "None  " : "Some  ") << good_age <<std::endl;

}

#include <iostream>
#include < algorithm >
#include <vector>
int main()
{
    std::vector<int> ages{ 22, 19, 46, 75, 54, 19, 27, 66, 61, 33, 22, 19 };

    int good_age{ 22 };
    std::cout << (std::all_of(std::begin(ages), std::end(ages), [good_age](int age) { return age < good_age; }) ? "None  " : "Some  ") << good_age <<std::endl;

}

 

2 any_of() 算法

        传入一个数组 good_age 来检测 ages 数组内是否存在比 good_age数组大的值

#include <iostream>
#include < algorithm >
#include <vector>
int main()
{
    std::vector<int> ages{ 22, 19, 46, 75, 54, 19, 27, 66, 61, 33, 22, 19 };

    int min_age{ 18 };
    std::cout << (std::any_of(std::begin(ages), std::end(ages), [min_age](int age) { return age < min_age; }) ? "yes "  : "no ") << min_age << std::endl;

}

#include <iostream>
#include < algorithm >
#include <vector>
int main()
{
    std::vector<int> ages{ 22, 19, 46, 75, 54, 19, 27, 66, 61, 33, 22, 19 };

    int min_age{ 100 };
    std::cout << (std::any_of(std::begin(ages), std::end(ages), [min_age](int age) { return age < min_age; }) ? "yes "  : "no ") << min_age << std::endl;

}

3 none_of() 算法

    传入一个数组 min_age 来检测 ages 数组内是否存在比 min_age数组大的值。

#include <iostream>
#include < algorithm >
#include <vector>
int main()
{
    std::vector<int> ages{ 22, 19, 46, 75, 54, 19, 27, 66, 61, 33, 22, 19 };
    int min_age{ 66 };
    std::cout << (std::none_of(std::begin(ages), std::end(ages), [min_age](int age) { return age < min_age; }) ? "NO" : "YES") << min_age << std::endl;
}

#include <iostream>
#include < algorithm >
#include <vector>
int main()
{
    std::vector<int> ages{ 22, 19, 46, 75, 54, 19, 27, 66, 61, 33, 22, 19 };
    int min_age{ 18 };
    std::cout << (std::none_of(std::begin(ages), std::end(ages), [min_age](int age) { return age < min_age; }) ? "NO" : "YES") << min_age << std::endl;
}

4 count count_if 范围查找

        可以快速查找出有多少数字大于输入值

#include <iostream>
#include < algorithm >
#include <vector>
int main()
{
    std::vector<int> ages{ 22, 19, 46, 75, 54, 19, 27, 66, 61, 33, 22, 19 };
    int the_age{ 19 };
    std::cout << "There are " << std::count(std::begin(ages), std::end(ages), the_age) << " people aged " << the_age << std::endl;
    int max_age{ 60 };
    std::cout << "There are " << std::count_if(std::begin(ages), std::end(ages), [max_age](int age) { return age > max_age; }) << " people aged over " << max_age << std::endl;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

双子座断点

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

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

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

打赏作者

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

抵扣说明:

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

余额充值