C++11 算法 检查区间元素

一 概述
  • C++ 标准库中提供了很多算法,定义于头文件 < algorithm >。本文主要探究以下用于 区间元素检查 的算法:
    • all_of(C++11) 检查一元谓词 p 是否对范围 [first, last) 中所有元素返回 true
    • any_of(C++11) 检查一元谓词 p 是否对范围 [first, last) 中至少一个元素返回 true
    • none_of(C++11) 检查一元谓词 p 是否不对范围 [first, last) 中任何元素返回 true
二 辅助函数
三 定义
template< class InputIt, class UnaryPredicate >
bool all_of( InputIt first, InputIt last, UnaryPredicate p );(1)(C++11)(C++20)
template< class InputIt, class UnaryPredicate >
constexpr bool all_of( InputIt first, InputIt last, UnaryPredicate p );(1)(C++20)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
bool all_of( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, 
             UnaryPredicate p );(2)(C++17)
template< class InputIt, class UnaryPredicate >
bool any_of( InputIt first, InputIt last, UnaryPredicate p );(3)(C++11)(C++20)
template< class InputIt, class UnaryPredicate >
constexpr bool any_of( InputIt first, InputIt last, UnaryPredicate p );(3)(C++20)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
bool any_of( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, 
             UnaryPredicate p );(4)(C++17)
template< class InputIt, class UnaryPredicate >
bool none_of( InputIt first, InputIt last, UnaryPredicate p );(5)(C++11)(C++20)
template< class InputIt, class UnaryPredicate >
constexpr bool none_of( InputIt first, InputIt last, UnaryPredicate p );(5)(C++20)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
bool none_of( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, 
              UnaryPredicate p );(6)(C++17)
四 Demo
  • 代码
std::vector<int> vc1{1,2,3};
std::vector<int> vc2{1,3,5};
std::vector<int> vc3{2,4,6};
std::vector<int> vc4;
print("vc1: ", vc1);
print("vc2: ", vc2);
print("vc3: ", vc3);
print("vc4: ", vc4);

// std::all_of
std::cout << " all_of  vc1 is "
          << (std::all_of(vc1.begin(), vc1.end(), even) ? "" : "not ")
          << "even" << std::endl;
std::cout << " all_of  vc2 is "
          << (std::all_of(vc2.begin(), vc2.end(), even) ? "" : "not ")
          << "even" << std::endl;
std::cout << " all_of  vc3 is "
          << (std::all_of(vc3.begin(), vc3.end(), even) ? "" : "not ")
          << "even" << std::endl;
std::cout << " all_of  vc4 is "
          << (std::all_of(vc4.begin(), vc4.end(), even) ? "" : "not ")
          << "even" << std::endl<< std::endl;

// std::any_of
std::cout << " any_of  vc1 is "
          << (std::any_of(vc1.begin(), vc1.end(), even) ? "" : "not ")
          << "even" << std::endl;
std::cout << " any_of  vc2 is "
          << (std::any_of(vc2.begin(), vc2.end(), even) ? "" : "not ")
          << "even" << std::endl;
std::cout << " any_of  vc3 is "
          << (std::any_of(vc3.begin(), vc3.end(), even) ? "" : "not ")
          << "even" << std::endl;
std::cout << " any_of  vc4 is "
          << (std::any_of(vc4.begin(), vc4.end(), even) ? "" : "not ")
          << "even" << std::endl << std::endl;

// std::none_of
std::cout << " none_of vc1 is "
          << (std::none_of(vc1.begin(), vc1.end(), even) ? "" : "not ")
          << "even" << std::endl;
std::cout << " none_of vc2 is "
          << (std::none_of(vc2.begin(), vc2.end(), even) ? "" : "not ")
          << "even" << std::endl;
std::cout << " none_of vc3 is "
          << (std::none_of(vc3.begin(), vc3.end(), even) ? "" : "not ")
          << "even" << std::endl;
std::cout << " none_of vc4 is "
          << (std::none_of(vc4.begin(), vc4.end(), even) ? "" : "not ")
          << "even" << std::endl << std::endl;
  • 结果
vc1: 1 2 3
vc2: 1 3 5
vc3: 2 4 6
vc4:

all_of  vc1 is not even
all_of  vc2 is not even
all_of  vc3 is even
all_of  vc4 is even // 结果1

any_of  vc1 is even
any_of  vc2 is not even
any_of  vc3 is even
any_of  vc4 is not even // 结果1

none_of vc1 is not even
none_of vc2 is even
none_of vc3 is not even
none_of vc4 is even // 结果1
  • 说明
    • 结果1, 注意当容器为空场景.(vs2017)
五 参考
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值