Cpp-standard-library-cpp-algorithm-copy
提供:Dev Guides
C ++アルゴリズムライブラリ-copy()関数
説明
- C ++関数* std
- algorithm :: copy()*は、ある範囲の要素を新しい場所にコピーします。
宣言
- 以下は、std
- algorithmヘッダーからのstd :: algorithm :: copy()関数の宣言です。
C 98
template <class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result);
パラメーター
- first -シーケンスの初期位置に反復子を入力します。
- last -シーケンスの最後の位置に反復子を入力します。
- result -新しいシーケンスの初期位置にイテレータを出力します。
戻り値
要素がコピーされた宛先範囲の末尾までの反復子を返します。
例外
要素の割り当てまたはイテレータの操作が例外をスローした場合、例外をスローします。
無効なパラメータは未定義の動作を引き起こすことに注意してください。
時間の複雑さ
`+ first `から ` last +`までの距離は線形です。
例
- 次の例は、std
- algorithm :: copy()関数の使用法を示しています。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void) {
vector<int> v1 = {1, 2, 3, 4, 5};
vector<int> v2(5);
copy(v1.begin(), v1.end(), v2.begin());
cout << "Vector v2 contains following elements" << endl;
for (auto it = v2.begin(); it != v2.end(); ++it)
cout << *it << endl;
return 0;
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Vector v2 contains following elements
1
2
3
4
5