Namespaces
Variants

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

From cppreference.net
(1)
vector ( ) : vector ( Allocator ( ) ) { }
(C++11以降)
(C++17まで)
vector ( ) noexcept ( noexcept ( Allocator ( ) ) ) : vector ( Allocator ( ) ) { }
(C++17以降)
(constexprはC++20以降)
(2)
explicit vector ( const Allocator & alloc = Allocator ( ) ) ;
(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以降)
(4)
explicit vector ( size_type count, const T & value = T ( ) ,
const Allocator & alloc = Allocator ( ) ) ;
(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)
(9)
vector ( const vector & other, const Allocator & alloc ) ;
(C++11以降)
(constexpr C++20以降)
(C++23まで)
constexpr vector ( const vector & other,
const std:: type_identity_t < Allocator > & alloc ) ;
(C++23以降)
(10)
vector ( vector && other, const Allocator & alloc ) ;
(C++11以降)
(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までのデフォルトコンストラクタ。指定されたアロケータ alloc で空の vector を構築します。
3) vector count 個のデフォルト挿入された T オブジェクトで構築します。コピーは作成されません。
T DefaultInsertable でない場合、動作は未定義です。
4) vector count 個の value 値を持つ要素で構築します。

T vector CopyInsertable でない場合、動作は未定義です。

(C++11以降)
5) 範囲 [ first , last ) の内容を持つ vector を構築します。 [ first , last ) 内の各イテレータは正確に1回デリファレンスされます。

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

(C++11以前)

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

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

(C++11以降)
6) 範囲 rg の内容を持つ vector を構築します。 rg 内の各イテレータは正確に一度だけデリファレンスされます。
以下のいずれかの条件が満たされる場合、動作は未定義です:
7) コピーコンストラクタ。 vector other の内容で構築します。

アロケータは、以下の呼び出しによって取得されます:
std:: allocator_traits < Allocator > :: select_on_container_copy_construction
( other. get_allocator ( ) )

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

目次

翻訳内容: - 「Contents」→「目次」 - その他のC++関連用語(Parameters, Complexity, Exceptions, Notes, Example, Defect reports, See also)は原文のまま保持 - HTMLタグ、属性、構造は完全に保持 - 番号部分は変更なし

パラメータ

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) Ranges-aware 構築と挿入; オーバーロード ( 6 )
__cpp_lib_ranges_reserve_hint 202502L (C++26) ranges::approximately_sized_range ranges::reserve_hint ; オーバーロード ( 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 ) において、コンテナ内の要素は
値初期化されていた
デフォルト挿入されるように変更

関連項目

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