Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

partition_point trong C++


Trong hướng dẫn này, chúng ta sẽ thảo luận về chương trình hiểu partition_point trong C++.

partition_point là phương thức trả về iterator trỏ đến giá trị đầu tiên trong một phạm vi cho trước. Phạm vi này là một phạm vi đã được phân hoạch (partitioned) mà trong đó predicate không còn đúng.

Ví dụ minh họa

#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; }

Kết quả

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