C++ 中的 partition_point


在本教程中,我们将讨论一个程序来了解 C++ 中的 partition_point。

partition_point 是一种方法,它返回一个指向给定范围中第一个值的迭代器。该范围是一个已分区的范围,其中谓词不为真。

示例

 实时演示

#include <iostream>
#include <algorithm>
#include <vector>
bool IsOdd(int i) { return (i % 2) == 1; }
int main(){
   std::vector<int> data{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   std::vector<int> odd, even;
   std::stable_partition(data.begin(), data.end(), IsOdd);
   auto it = std::partition_point(data.begin(), data.end(),
   IsOdd);
   odd.assign(data.begin(), it);
   even.assign(it, data.end());
   std::cout << "odd:";
   for (int& x : odd)
      std::cout << ' ' << x;
   std::cout << '\n';
   std::cout << "even:";
   for (int& x : even)
      std::cout << ' ' << x;
   std::cout << '\n';
   return 0;
}

输出

odd: 1 3 5 7 9
even: 2 4 6 8 10

更新于:14 年 4 月 2020 日

102 次浏览

开启你的 职业生涯

完成课程获取证书

开始吧
广告
© . All rights reserved.