Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

partition_point ใน C++


ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมเพื่อทำความเข้าใจ partition_point ใน C++

จุดแบ่งพาร์ติชันเป็นวิธีที่ส่งกลับตัววนซ้ำที่ชี้ไปที่ค่าแรกในช่วงที่กำหนด ช่วงเป็นช่วงที่แบ่งพาร์ติชั่นซึ่งภาคแสดงไม่เป็นความจริง

ตัวอย่าง

#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