Namespaces
Variants

std::vector<T,Allocator>::vector

ja.cppreference.net より
 
 
 
 
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 を使用します。

1) C++11 からのデフォルトコンストラクタ。デフォルト構築されたアロケータを持つ空の vector を構築します。
Allocator が DefaultConstructible でない場合、動作は未定義です。
2) C++11 までのデフォルトコンストラクタ。指定されたアロケータ vector を持つ空の alloc を構築します。
3) vector のデフォルト挿入されたオブジェクトを count 個持つ T を構築します。コピーは行われません。
T が DefaultInsertable でない場合 std::vector<T>、動作は未定義です。
4) 値 vector の要素を count 個コピーしたものを持つ value を構築します。

T が CopyInsertable でない場合 std::vector<T>、動作は未定義です。

(C++11 から)
5) 範囲 vector の内容を持つ [first, last) を構築します。[first, last) 内の各イテレータはちょうど1回デリファレンスされます。

InputIt が LegacyInputIterator の要件を満たさない場合、オーバーロード (4) が引数 static_cast<size_type>(first), last, alloc で代わりに呼び出されます。

(C++11 まで)

このオーバーロードは、InputIt が LegacyInputIterator の要件を満たす場合にのみオーバーロード解決に参加します。

以下の条件のいずれかが満たされる場合、動作は未定義です:

(C++11 から)
6) 範囲 vector の内容を持つ rg を構築します。rg 内の各イテレータはちょうど1回デリファレンスされます。
以下の条件のいずれかが満たされる場合、動作は未定義です:
7-10) vector の内容を持つ other を構築します。
7) コピーコンストラクタ。

アロケータは std::allocator_traits<Allocator>::select_on_container_copy_construction(alloc) を呼び出すことにより取得されます。ここで alloc は other のアロケータです。

(C++11 から)
8) ムーブコンストラクタ。other.get_allocator() からのムーブ構築によりアロケータが取得されます。
9) コピーコンストラクタと同様ですが、アロケータとして alloc が使用されます。
T が CopyInsertable でない場合 std::vector<T>、動作は未定義です。
10) ムーブコンストラクタと同様ですが、アロケータとして alloc が使用されます。
T が MoveInsertable でない場合 std::vector<T>、動作は未定義です。
11) vector(il.begin(), il.end(), alloc) と同等です。

パラメータ

alloc - このコンテナのすべてのメモリ割り当てに使用するアロケータ
count - コンテナのサイズ
value - コンテナの要素を初期化するための値
first, last - コピーする要素の 範囲 を定義するイテレータのペア
other - コンテナの要素を初期化するためのソースとして使用する別のコンテナ
init - コンテナの要素を初期化するための初期化子リスト
rg - コンテナ互換の範囲

計算量

1,2) 定数。
3,4) 線形( count に比例)。
5) 与えられる std:: distance ( first, last ) を N とする:
  • T のコピーコンストラクタは N 回のみ呼び出される。
  • 再確保は発生しない。
  • それ以外の場合:
  • T のコピーコンストラクタは O(N) 回呼び出される。
  • 再確保は O(log N) 回発生する。
6) 与えられる ranges:: distance ( rg ) を N として:
(C++26まで)
  • R が以下の条件のいずれかを満たす場合:
の場合:
(C++26から)
  • rg の連続するイテレータをデリファレンスした結果から正確に N 個の要素を初期化する
  • 再確保は発生しない
  • それ以外の場合:
  • T のコピーコンストラクタまたはムーブコンストラクタが O(N) 回呼び出される
  • 再確保が O(log N) 回発生する
7) 線形で other. size ( ) の計算量です。
8) 定数。
9) 線形で other. size ( ) の計算量です。
10) other. size ( ) に対して線形、 alloc ! = other. get_allocator ( ) の場合、それ以外の場合は定数。
11) 線形で init. size ( ) 。

例外

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 ) において、コンテナ内の要素は
値初期化されていた
デフォルト挿入されるように変更

関連項目

コンテナに値を割り当てる
(公開メンバ関数)
コンテナに値を割り当てる
(公開メンバ関数)