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()算法
传入一个数组 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;
}