std::uninitialized_copy_n - cppreference.com
提供: cppreference.com
<tbody> </tbody>
| ヘッダ |
||
|
|
(1) | (C++11以上) |
|
|
(2) | (C++17以上) |
1) 以下のように行われたかのように、 first から始まる範囲の count 個の要素を d_first から始まる未初期化メモリ領域にコピーします。
for ( ; count > 0; ++d_first, (void) ++first, --count) ::new (static_cast<void*>(std::addressof(*d_first))) typename std::iterator_traits<ForwardIt>::value_type(*first);
初期化中に例外が投げられた場合、すでに構築されたオブジェクトは未規定の順序で破棄されます。
2) (1) と同じですが、 policy に従って実行されます。 このオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true でなければ、オーバーロード解決に参加しません。
引数
| first | - | コピーする要素の範囲の先頭 |
| count | - | コピーする要素の数 |
| d_first | - | コピー先の範囲の先頭 |
| policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
| 型の要件 | ||
-InputIt は LegacyInputIterator の要件を満たさなければなりません。
| ||
-ForwardIt は LegacyForwardIterator の要件を満たさなければなりません。
| ||
-ForwardIt の有効なインスタンスのインクリメント、代入、比較、間接参照は例外を投げてはなりません。
| ||
戻り値
コピーされた最後の要素の次の要素を指すイテレータ。
計算量
count に比例。
例外
テンプレート引数 ExecutionPolicy を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicyが標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆるExecutionPolicyについては、動作は処理系定義です。 - アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。
実装例
template<class InputIt, class Size, class ForwardIt> ForwardIt uninitialized_copy_n(InputIt first, Size count, ForwardIt d_first) { typedef typename std::iterator_traits<ForwardIt>::value_type Value; ForwardIt current = d_first; try { for (; count > 0; ++first, (void) ++current, --count) { ::new (static_cast<void*>(std::addressof(*current))) Value(*first); } } catch (...) { for (; d_first != current; ++d_first) { d_first->~Value(); } throw; } return current; }
例
#include <algorithm> #include <iostream> #include <memory> #include <string> #include <tuple> #include <vector> int main() { std::vector<std::string> v = {"This", "is", "an", "example"}; std::string* p; std::size_t sz; std::tie(p, sz) = std::get_temporary_buffer<std::string>(v.size()); sz = std::min(sz, v.size()); std::uninitialized_copy_n(v.begin(), sz, p); for (std::string* i = p; i != p+sz; ++i) { std::cout << *i << ' '; i->~basic_string<char>(); } std::return_temporary_buffer(p); }
出力: