std::vector<T,Allocator>::vector - cppreference.com
提供: cppreference.com
<tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody>
| (1) | ||
|
|
(C++17未満) | |
|
|
(C++17以上) | |
| (2) | ||
|
|
(C++17未満) | |
|
|
(C++17以上) | |
| (3) | ||
|
|
(C++11未満) | |
|
|
(C++11以上) | |
| (4) | ||
|
|
(C++11以上) (C++14未満) |
|
|
|
(C++14以上) | |
|
|
(5) | |
|
|
(6) | |
|
|
(6) | (C++11以上) |
| (7) | ||
|
|
(C++11以上) (C++17未満) |
|
|
|
(C++17以上) | |
|
|
(8) | (C++11以上) |
|
|
(9) | (C++11以上) |
様々なデータソースから新しいコンテナを構築します。 オプションでユーザ提供のアロケータ alloc を使用します。
1) デフォルトコンストラクタ。 デフォルト構築されたアロケータを使用して空のコンテナを構築します。
2) 指定されたアロケータ alloc を使用して空のコンテナを構築します。
3) 値 value を持つ要素の count 個のコピーを持つコンテナを構築します。
4) T のデフォルト挿入された count 個のインスタンスを持つコンテナを構築します。 コピーは行われません。
5) 範囲 [first, last) の内容を持つコンテナを構築します。
InputIt が整数型の場合、このコンストラクタは vector(static_cast<size_type>(first), static_cast<value_type>(last), a) と同じ効果を持ちます。 |
(C++11未満) |
このオーバーロードは、オーバーロード (3) との曖昧さを回避するため、 InputIt が LegacyInputIterator を満たす場合にのみ、オーバーロード解決に参加します。 |
(C++11以上) |
6) コピーコンストラクタ。 other の内容のコピーを持つコンテナを構築します。 alloc が提供されない場合、アロケータは std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()) を呼んだかのように取得されます。
7) ムーブコンストラクタ。 ムーブセマンティクスを用いて other の内容を持つコンテナを構築します。 アロケータは other の持つアロケータからムーブ構築によって取得されます。 ムーブ後、 other は empty() であることが保証されます。
8) アロケータ拡張ムーブコンストラクタ。 新しいコンテナのためのアロケータとして alloc を使用し、 other の内容をムーブします。 alloc != other.get_allocator() の場合は、要素単位のムーブになります(その場合、ムーブ後に other が空になる保証はありません)。
9) 初期化子リスト init の内容を持つコンテナを構築します。
引数
| alloc | - | このコンテナのすべてのメモリ確保のために使用するアロケータ |
| count | - | コンテナのサイズ |
| value | - | コンテナの要素を初期化するための値 |
| first, last | - | 要素のコピー元の範囲 |
| other | - | コンテナの要素を初期化するソースとして使用する別のコンテナ |
| init | - | コンテナの要素を初期化するための初期化子リスト |
計算量
1-2) 一定。
3-4) count に比例。
5) first と last の距離に比例。
6) other のサイズに比例。
7) 一定。
8) alloc != other.get_allocator() の場合は other のサイズに比例、そうでなければ一定。
9) init のサイズに比例。
例外
Allocator::allocate の呼び出しが例外を投げる可能性があります。
ノート
コンテナのムーブ構築 (オーバーロード (7)) の後、 other を指す参照、ポインタ、イテレータ (終端イテレータは除く) は有効なまま残りますが、以後 *this 内の要素を指すようになります。 現行の標準ではこの保証は [container.requirements.general]/12 の包括的な文言によってなされていますが、より直接的な保証が LWG 2321 で検討されています。
オーバーロード (4) は int のような非クラス型の要素をゼロクリアします。 これは要素を初期化しない new[] の動作と異なります。 new[] の動作と一致させるためには、要素を初期化しないカスタムな Allocator::construct を使用できます。
例
#include <vector> #include <string> #include <iostream> template<typename T> std::ostream& operator<<(std::ostream& s, const std::vector<T>& v) { s.put('['); char comma[3] = {'\0', ' ', '\0'}; for (const auto& e : v) { s << comma << e; comma[0] = ','; } return s << ']'; } int main() { // C++11 の初期化子リストの構文。 std::vector<std::string> words1 {"the", "frogurt", "is", "also", "cursed"}; std::cout << "words1: " << words1 << '\n'; // words2 == words1 std::vector<std::string> words2(words1.begin(), words1.end()); std::cout << "words2: " << words2 << '\n'; // words3 == words1 std::vector<std::string> words3(words1); std::cout << "words3: " << words3 << '\n'; // words4 is {"Mo", "Mo", "Mo", "Mo", "Mo"} std::vector<std::string> words4(5, "Mo"); std::cout << "words4: " << words4 << '\n'; }
出力:
words1: [the, frogurt, is, also, cursed] words2: [the, frogurt, is, also, cursed] words3: [the, frogurt, is, also, cursed] words4: [Mo, Mo, Mo, Mo, Mo]
欠陥報告
以下の動作変更欠陥報告は以前に発行された C++ 標準に遡って適用されました。
| DR | 適用先 | 発行時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 2193 | C++11 | the default constructor is explicit | made non-explicit |