C++ any_of用法学习
转自:https://www.cnblogs.com/pandamohist/p/13854504.html,https://cplusplus.com/reference/algorithm/any_of/
1.介绍
template <class InputIterator, class UnaryPredicate> bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred);
区间[开始, 结束)中是否至少有一个元素都满足判断式p,只要有一个元素满足条件就返回true,否则返回true。
函数功能:
template<class InputIterator, class UnaryPredicate> bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred) { while (first!=last) {// 在区间内查找 if (pred(*first)) return true;// 符合条件返回true ++first; } return false; }
2.例子
// any_of example #include <iostream> // std::cout #include <algorithm> // std::any_of #include <array> // std::array int main () { std::array<int,7> foo = {0,1,-1,3,-3,5,-5}; if ( std::any_of(foo.begin(), foo.end(), [](int i){return i<0;}) ) std::cout << "There are negative elements in the range.\n"; return 0; } // 输出 There are negative elements in the range.
可能会抛出异常。 另外还有:
all_of:区间[开始, 结束)中是否所有的元素都满足判断式p,所有的元素都满足条件返回true,否则返回false。
none_of:区间[开始, 结束)中是否所有的元素都不满足判断式p,所有的元素都不满足条件返回true,否则返回false。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2021-06-04 二进制与十进制的转换
2020-06-04 基于DL的文本分类综述