copy を使ってみる (C++)
C++
Published: 2019-12-19

やったこと

copy を使って、vector をコピーしてみます。

確認環境

$ g++ --version
g++ (Homebrew GCC 9.2.0) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

調査

test.cpp

#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v1 = {3, 10, 2};
    vector<int> v2(v1.size());
    copy(v1.begin(), v1.end(), v2.begin());

    sort(v1.begin(), v1.end());

    for (auto i : v1) {
        cout << i << " ";
    }
    cout << endl;

    for (auto i : v2) {
        cout << i << " ";
    }
    cout << endl;
}

出力結果

2 3 10
3 10 2

参考