std::vector<T,Allocator>::vector
vector() : vector(Allocator()) {}
|
(1) | (C++11 から) (C++17 まで) |
vector() noexcept(noexcept(Allocator())) : vector(Allocator()) {}
|
(C++17 から) (C++20 から constexpr) |
|
explicit vector( const Allocator& alloc = Allocator() );
|
(2) | (C++11 まで) |
explicit vector( const Allocator& alloc );
|
(C++11 から) (C++17 から noexcept) (C++20 から constexpr) |
|
explicit vector( size_type count,
const Allocator& alloc = Allocator() );
|
(3) | (C++11 から) |
explicit vector( size_type count, const T& value = T(),
const Allocator& alloc = Allocator() );
|
(4) | (C++11 まで) |
vector( size_type count, const T& value,
const Allocator& alloc = Allocator() );
|
(C++11 から) (C++20 から constexpr) |
|
template< class InputIt >
vector( InputIt first, InputIt last,
const Allocator& alloc = Allocator() );
|
(5) | (C++20 から constexpr) |
template< container-compatible-range<T> R >
constexpr vector( std::from_range_t, R&& rg,
const Allocator& alloc = Allocator() );
|
(6) | (C++23 から) |
vector( const vector& other );
|
(7) | (C++20 から constexpr) |
vector( vector&& other );
|
(8) | (C++11 から) (C++17 から noexcept) (C++20 から constexpr) |
vector( const vector& other, const Allocator& alloc );
|
(9) | (C++11 から) (C++20 から constexpr) (C++23 まで) |
constexpr vector( const vector& other,
const std::type_identity_t<Allocator>& alloc );
|
(C++23 から) | |
vector( vector&& other, const Allocator& alloc );
|
(10) | (C++11 から) (C++20 から constexpr) (C++23 まで) |
constexpr vector( vector&& other,
const std::type_identity_t<Allocator>& alloc );
|
(C++23 から) | |
vector( std::initializer_list<T> init,
const Allocator& alloc = Allocator() );
|
(11) | (C++11 から) |
様々なデータソースから新しい vector を構築します。オプションでユーザー指定のアロケータ alloc を使用します。
vector を構築します。Allocator が DefaultConstructible でない場合、動作は未定義です。vector を持つ空の alloc を構築します。vector のデフォルト挿入されたオブジェクトを count 個持つ T を構築します。コピーは行われません。vector の要素を count 個コピーしたものを持つ value を構築します。
|
|
(C++11 から) |
vector の内容を持つ [first, last) を構築します。[first, last) 内の各イテレータはちょうど1回デリファレンスされます。
|
|
(C++11 まで) |
|
このオーバーロードは、 以下の条件のいずれかが満たされる場合、動作は未定義です:
|
(C++11 から) |
vector の内容を持つ rg を構築します。rg 内の各イテレータはちょうど1回デリファレンスされます。Tが EmplaceConstructible でない場合std::vector<T>から*ranges::begin(rg)。Rが sized_range も forward_range もモデル化せず、かつTが MoveInsertable でない場合std::vector<T>。
vector の内容を持つ other を構築します。|
アロケータは |
(C++11 から) |
other.get_allocator() からのムーブ構築によりアロケータが取得されます。alloc が使用されます。alloc が使用されます。vector(il.begin(), il.end(), alloc) と同等です。パラメータ
| alloc | - | このコンテナのすべてのメモリ割り当てに使用するアロケータ |
| count | - | コンテナのサイズ |
| value | - | コンテナの要素を初期化するための値 |
| first, last | - | コピーする要素の 範囲 を定義するイテレータのペア |
| other | - | コンテナの要素を初期化するためのソースとして使用する別のコンテナ |
| init | - | コンテナの要素を初期化するための初期化子リスト |
| rg | - | コンテナ互換の範囲 |
計算量
-
InputItが LegacyForwardIterator の要件を満たす場合:
-
-
Tのコピーコンストラクタは N 回のみ呼び出される。 - 再確保は発生しない。
-
- それ以外の場合:
-
-
Tのコピーコンストラクタは O(N) 回呼び出される。 - 再確保は O(log N) 回発生する。
-
|
(C++26まで) |
|
(C++26から) |
-
- rg の連続するイテレータをデリファレンスした結果から正確に N 個の要素を初期化する
- 再確保は発生しない
- それ以外の場合:
-
-
Tのコピーコンストラクタまたはムーブコンストラクタが O(N) 回呼び出される - 再確保が O(log N) 回発生する
-
例外
Allocator :: allocate の呼び出しは例外をスローする可能性があります。
注記
コンテナのムーブ構築(オーバーロード (8))の後、元々 other 内の要素を参照していた参照、ポインタ、イテレータは有効なままですが、現在は *this 内の要素を参照するようになります。現在の標準は、[container.reqmts]/67 の包括的な記述を通じてこの保証を行っており、より直接的な保証は LWG issue 2321 を通じて検討中です。
| 機能テストマクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_containers_ranges |
202202L |
(C++23) | Range対応の構築と挿入; オーバーロード (6) |
例
#include <iostream> #include <string> #include <vector> template<typename T> std::ostream& operator<<(std::ostream& s, const std::vector<T>& v) { s.put('{'); for (char comma[]{'\0', ' ', '\0'}; const auto& e : v) s << comma << e, comma[0] = ','; return s << "}\n"; } int main() { // C++11 初期化リスト構文: std::vector<std::string> words1{"the", "frogurt", "is", "also", "cursed"}; std::cout << "1: " << words1; // words2 == words1 std::vector<std::string> words2(words1.begin(), words1.end()); std::cout << "2: " << words2; // words3 == words1 std::vector<std::string> words3(words1); std::cout << "3: " << words3; // words4 は {"Mo", "Mo", "Mo", "Mo", "Mo"} std::vector<std::string> words4(5, "Mo"); std::cout << "4: " << words4; const auto rg = {"cat", "cow", "crow"}; #ifdef __cpp_lib_containers_ranges std::vector<std::string> words5(std::from_range, rg); // オーバーロード (6) #else std::vector<std::string> words5(rg.begin(), rg.end()); // オーバーロード (5) #endif std::cout << "5: " << words5; }
出力:
1: {the, frogurt, is, also, cursed}
2: {the, frogurt, is, also, cursed}
3: {the, frogurt, is, also, cursed}
4: {Mo, Mo, Mo, Mo, Mo}
5: {cat, cow, crow}
欠陥報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 修正後の動作 |
|---|---|---|---|
| LWG 134 | C++98 |
オーバーロード
(
5
)
は入力イテレータの場合に最大
2N
回のコピー
コンストラクタ呼び出しを許可していた |
O(N) 回の呼び出しに変更 |
| LWG 438 | C++98 |
オーバーロード
(
5
)
は
InputIt
が整数型の場合のみ
オーバーロード ( 4 ) を呼び出していた |
InputIt
が
LegacyInputIterator
でない場合に
オーバーロード ( 4 ) を呼び出す |
| LWG 2193 | C++11 | デフォルトコンストラクタがexplicitであった | non-explicitに変更 |
| LWG 2210 | C++11 | オーバーロード ( 3 ) にアロケータパラメータがなかった | パラメータを追加 |
| N3346 | C++11 |
オーバーロード
(
3
)
において、コンテナ内の要素は
値初期化されていた |
デフォルト挿入されるように変更 |
関連項目
|
コンテナに値を割り当てる
(公開メンバ関数) |
|
|
コンテナに値を割り当てる
(公開メンバ関数) |