C++11 算法 区间是否被分割

一 概述
  • C++ 标准库中提供了很多算法,定义于头文件 < algorithm >。本文主要探究以下用于 区间是否被分割判断 的算法:
    • is_partitioned(C++11) 区间是否按谓词进行分割
    • partition_point(C++11) 返回已分割区间的分割点
二 辅助函数
三 std::is_partitioned(C++11)
  • 判断区间是否按谓词进行分割。简单的说:就是是否满足条件的均在区间前面,不满足的均在区间后面。
  • 定义
template< class InputIt, class UnaryPredicate >
bool is_partitioned( InputIt first, InputIt last, UnaryPredicate p );
                   (1)(C++11)(C++20)
template< class InputIt, class UnaryPredicate >
constexpr bool is_partitioned( InputIt first, InputIt last,
                               UnaryPredicate p );(1)(C++20)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
bool is_partitioned( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last,
                     UnaryPredicate p );(2)(C++17)
四 std::partition_point(C++11)
  • 返回已分割区间的分割点
  • 定义
template< class ForwardIt, class UnaryPredicate >
ForwardIt partition_point( ForwardIt first, ForwardIt last, 
                           UnaryPredicate p );(C++11)(C++20)
template< class ForwardIt, class UnaryPredicate >
constexpr ForwardIt partition_point( ForwardIt first, ForwardIt last, 
                                     UnaryPredicate p );(C++20)
五 Demo
std::vector<int> vc1{1,3,5,2,4,6};
std::vector<int> vc2{2,4,6,1,3,5};
std::vector<int> vc3;
std::vector<int> vc4{2,4,6,8};;
fill(vc3, 1, 6);

print("vc1: ", vc1);
print("vc2: ", vc2);
print("vc3: ", vc3);
print("vc3: ", vc4);
// std::is_partitioned(C++11)
std::cout << "vc1 is "
          << (std::is_partitioned(vc1.begin(), vc1.end(), even) ? "" : "not ")
          << "partitioned" << std::endl;
std::cout << "vc2 is "
          << (std::is_partitioned(vc2.begin(), vc2.end(), even) ? "" : "not ")
          << "partitioned" << std::endl;
std::cout << "vc3 is "
          << (std::is_partitioned(vc3.begin(), vc3.end(), even) ? "" : "not ")
          << "partitioned" << std::endl;
std::cout << "vc4 is "
          << (std::is_partitioned(vc4.begin(), vc4.end(), even) ? ""
                                                                : "not ")
          << "partitioned" << std::endl;
          
// std::partition_point(C++11)
auto it1 = std::partition_point(vc1.begin(), vc1.end(), even);
std::cout << "vc1: the No." << std::distance(vc1.begin(), it1) 
          << " is partition_point " << std::endl;
auto it2 = std::partition_point(vc2.begin(), vc2.end(), even);
std::cout << "vc2: the No." << std::distance(vc2.begin(), it2)
          << " is partition_point" << std::endl;
auto it3 = std::partition_point(vc3.begin(), vc3.end(), even);
std::cout << "vc3: the No." << std::distance(vc3.begin(), it3)
          << " is partition_point" << std::endl;
auto it4 = std::partition_point(vc4.begin(), vc4.end(), even);
std::cout << "vc4: the No." << std::distance(vc4.begin(), it4)
          << " is partition_point " << std::endl;
  • 结果
vc1: 1 3 5 2 4 6
vc2: 2 4 6 1 3 5
vc3: 1 2 3 4 5 6
vc3: 2 4 6 8

vc1 is not partitioned // 结果 1
vc2 is partitioned // 结果 1
vc3 is not partitioned
vc4 is partitioned

vc1: the No.6 is partition_point // 结果 2
vc2: the No.3 is partition_point
vc3: the No.6 is partition_point // 结果 2
vc4: the No.4 is partition_point // 结果 3
  • 说明
    • 结果1,满足结果的在前,不满足的再后,不能反过来。
    • 结果2, 对于不满足 std::is_partitioned 的区间,std::partition_point 的返回不同编译器可能不同,当前(vs2017)中返回end()。
    • 结果3, 对于所有元素满足条件的区间,std::partition_point 返回 end()。
六 参考
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值