名稱空間
變體
操作

std::ranges::partition_copy, std::ranges::partition_copy_result

來自 cppreference.com
< cpp‎ | 演算法‎ | 範圍
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
受約束演算法
此選單中的所有名稱均屬於名稱空間 std::ranges
非修改序列操作
修改序列操作
劃分操作
partition_copy
  
排序操作
二分查詢操作(在已排序的範圍內)
       
       
集合操作(於已排序範圍上)
堆操作
最小/最大值操作
       
       
排列操作
摺疊操作
數值操作
(C++23)            
對未初始化儲存的操作
返回型別
 
定義於標頭檔案 <algorithm>
呼叫簽名 (Call signature)
template< std::input_iterator I, std::sentinel_for<I> S,

          std::weakly_incrementable O1, std::weakly_incrementable O2,
          class Proj = std::identity,
          std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
requires std::indirectly_copyable<I, O1> &&
         std::indirectly_copyable<I, O2>
constexpr partition_copy_result<I, O1, O2>
    partition_copy( I first, S last, O1 out_true, O2 out_false,

                    Pred pred, Proj proj = {} );
(1) (C++20 起)
template< ranges::input_range R,

          std::weakly_incrementable O1, std::weakly_incrementable O2,
          class Proj = std::identity,
          std::indirect_unary_predicate<std::projected<iterator_t<R>, Proj>> Pred >
requires std::indirectly_copyable<ranges::iterator_t<R>, O1> &&
         std::indirectly_copyable<ranges::iterator_t<R>, O2>
constexpr partition_copy_result<ranges::borrowed_iterator_t<R>, O1, O2>
    partition_copy( R&& r, O1 out_true, O2 out_false,

                    Pred pred, Proj proj = {} );
(2) (C++20 起)
輔助型別
template< class I, class O1, class O2 >
using partition_copy_result = ranges::in_out_out_result<I, O1, O2>;
(3) (C++20 起)
1) 將輸入範圍 [firstlast) 中的元素根據謂詞 pred 的返回值複製到兩個不同的輸出範圍。經過 proj 投影后滿足謂詞 pred 的元素被複制到始於 out_true 的範圍。其餘元素被複制到始於 out_false 的範圍。如果輸入範圍與任何一個輸出範圍重疊,則行為未定義。
2)(1),但使用 r 作為源範圍,如同使用 ranges::begin(r) 作為 first,並使用 ranges::end(r) 作為 last

本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即

目錄

[編輯] 引數

first, last - 定義要複製的源元素範圍的迭代器-哨兵對
r - 要複製的元素的源範圍
out_true - 滿足 pred 的元素的輸出範圍的起始
out_false - 不滿足 pred 的元素的輸出範圍的起始
pred - 應用於投影元素的謂詞
proj - 應用於元素的投影

[編輯] 返回值

{last, o1, o2},其中 o1o2 分別是複製完成後輸出範圍的末尾。

[編輯] 複雜度

對相應謂詞 comp 和任何投影 proj 進行精確 ranges::distance(first, last) 次應用。

[編輯] 可能的實現

struct partition_copy_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S,
             std::weakly_incrementable O1, std::weakly_incrementable O2,
             class Proj = std::identity, std::indirect_unary_predicate<
             std::projected<I, Proj>> Pred>
    requires std::indirectly_copyable<I, O1> && std::indirectly_copyable<I, O2>
    constexpr ranges::partition_copy_result<I, O1, O2>
        operator()(I first, S last, O1 out_true, O2 out_false,
                   Pred pred, Proj proj = {}) const
    {
        for (; first != last; ++first)
            if (!!std::invoke(pred, std::invoke(proj, *first)))
                *out_true = *first, ++out_true;
            else
                *out_false = *first, ++out_false;
        return {std::move(first), std::move(out_true), std::move(out_false)};
    }
 
    template<ranges::input_range R,
             std::weakly_incrementable O1, std::weakly_incrementable O2,
             class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<iterator_t<R>, Proj>> Pred>
    requires std::indirectly_copyable<ranges::iterator_t<R>, O1> &&
             std::indirectly_copyable<ranges::iterator_t<R>, O2>
    constexpr ranges::partition_copy_result<ranges::borrowed_iterator_t<R>, O1, O2>
        operator()(R&& r, O1 out_true, O2 out_false, Pred pred, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(out_true),
                       std::move(out_false), std::move(pred), std::move(proj));
    }
};
 
inline constexpr partition_copy_fn partition_copy {};

[編輯] 示例

#include <algorithm>
#include <cctype>
#include <iostream>
#include <iterator>
#include <vector>
 
int main()
{
    const auto in = {'N', '3', 'U', 'M', '1', 'B', '4', 'E', '1', '5', 'R', '9'};
 
    std::vector<int> o1(size(in)), o2(size(in));
 
    auto pred = [](char c) { return std::isalpha(c); };
 
    auto ret = std::ranges::partition_copy(in, o1.begin(), o2.begin(), pred);
 
    std::ostream_iterator<char> cout {std::cout, " "};
    std::cout << "in = ";
    std::ranges::copy(in, cout);
    std::cout << "\no1 = ";
    std::copy(o1.begin(), ret.out1, cout);
    std::cout << "\no2 = ";
    std::copy(o2.begin(), ret.out2, cout);
    std::cout << '\n';
}

輸出

in = N 3 U M 1 B 4 E 1 5 R 9
o1 = N U M B E R
o2 = 3 1 4 1 5 9

[編輯] 參閱

將一個範圍的元素分成兩組
(演算法函式物件)[編輯]
將元素分成兩組,同時保留它們的相對順序
(演算法函式物件)[編輯]
將一個範圍的元素複製到一個新位置
(演算法函式物件)[編輯]
複製一個範圍的元素,忽略那些滿足特定條件的元素
(演算法函式物件)[編輯]
複製一個範圍,並將元素分成兩組
(函式模板) [編輯]