std::partial_sort_copy は入力側のシーケンスを変更しない

試した環境

本題

std::partial_sort_copy は範囲を部分的にソートした結果を他の範囲にコピーする処理を行います。 標準ライブラリの他のソート処理 (std::sort や std::partial_sort など) と違い、入力のシーケンスとコピー先のシーケンスとの2つのシーケンスを指定します。 std::sort や std::partial_sort では入力のシーケンスに対してソートが行われますが、std::partial_sort_copy は入力のシーケンスがソートされるのか、リファレンスを読んでも自信が持てなかったので確認しました。

以下のコードで試しました。

#include <algorithm>
#include <iostream>
#include <numeric>
#include <random>
#include <vector>

int main() {
    std::random_device seed_gen;
    std::default_random_engine engine(seed_gen());

    std::vector<int> input(16);
    std::iota(input.begin(), input.end(), 0);
    std::shuffle(input.begin(), input.end(), engine);

    std::cout << "before:";
    std::for_each(input.begin(), input.end(), [](int x) {
        std::cout << ' ' << x;
        });
    std::cout << std::endl;

    std::vector<int> result(8);
    std::partial_sort_copy(input.begin(), input.end(), result.begin(), result.end());

    std::cout << "after:";
    std::for_each(input.begin(), input.end(), [](int x) {
        std::cout << ' ' << x;
        });
    std::cout << std::endl;
    std::cout << "result:";
    std::for_each(result.begin(), result.end(), [](int x) {
        std::cout << ' ' << x;
        });
    std::cout << std::endl;
    return 0;
}

結果は以下の通りです。 入力側のシーケンスがソートされていないことがわかります。

$ x64\Release\partial_sort_copy.exe
before: 9 12 0 7 2 5 11 3 10 15 14 8 13 4 1 6
after: 9 12 0 7 2 5 11 3 10 15 14 8 13 4 1 6
result: 0 1 2 3 4 5 6 7

wandbox での結果も載せます。

https://wandbox.org/permlink/a6020Bl1AZAVeQZF

参考

partial_sort_copy - cpprefjp C++日本語リファレンス