Namespaces
Variants

std::list<T,Allocator>:: list

From cppreference.net
list ( ) : list ( Allocator ( ) ) { }
(1) (C++11以降)
(constexpr C++26以降)
(2)
explicit list ( const Allocator & alloc = Allocator ( ) ) ;
(C++11まで)
explicit list ( const Allocator & alloc ) ;
(C++11以降)
(constexpr C++26以降)
explicit list ( size_type count,
const Allocator & alloc = Allocator ( ) ) ;
(3) (C++11以降)
(constexpr C++26以降)
(4)
explicit list ( size_type count, const T & value = T ( ) ,
const Allocator & alloc = Allocator ( ) ) ;
(C++11まで)
list ( size_type count, const T & value,
const Allocator & alloc = Allocator ( ) ) ;
(C++11以降)
(C++26以降 constexpr)
template < class InputIt >

list ( InputIt first, InputIt last,

const Allocator & alloc = Allocator ( ) ) ;
(5) (C++26以降 constexpr)
template < container-compatible-range < T > R >

list ( std:: from_range_t , R && rg,

const Allocator & alloc = Allocator ( ) ) ;
(6) (C++23以降)
(C++26以降 constexpr)
list ( const list & other ) ;
(7) (C++26以降 constexpr)
list ( list && other ) ;
(8) (C++11以降)
(C++26以降 constexpr)
(9)
list ( const list & other, const Allocator & alloc ) ;
(C++11以降)
(C++23まで)
list ( const list & other,
const std:: type_identity_t < Allocator > & alloc ) ;
(C++23以降)
(constexpr C++26以降)
(10)
list ( list && other, const Allocator & alloc ) ;
(C++11以降)
(C++23まで)
list ( list && other, const std:: type_identity_t < Allocator > & alloc ) ;
(C++23以降)
(C++26以降constexpr)
list ( std:: initializer_list < T > init,
const Allocator & alloc = Allocator ( ) ) ;
(11) (C++11以降)
(C++26以降 constexpr)

様々なデータソースから新しい list を構築します。オプションでユーザー提供のアロケータ alloc を使用します。

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

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

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

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

(C++11以前)

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

T * first から list EmplaceConstructible でない場合、動作は未定義です。

(C++11以降)
6) 範囲 rg の内容を持つ list を構築します。 rg 内の各イテレータは正確に一度だけデリファレンスされます。
Tが T から * ranges:: begin ( rg ) によって list EmplaceConstructible でない場合、動作は未定義です。
7) コピーコンストラクタ。 list other の内容で構築する。

アロケータは、 std:: allocator_traits < Allocator > :: select_on_container_copy_construction
( other. get_allocator ( ) )
を呼び出したかのように取得される。

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

目次

パラメータ

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

計算量

1,2) 定数。
3,4) 線形 count 回。
5) 線形時間( std:: distance ( first, last ) の計算量)。
6) ranges:: distance ( rg ) において線形。
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) レンジ対応 構築と挿入; オーバーロード ( 6 )

#include <iostream>
#include <list>
#include <string>
template<typename T>
std::ostream& operator<<(std::ostream& s, const std::list<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::list<std::string> words1{"the", "frogurt", "is", "also", "cursed"};
    std::cout << "1: " << words1;
    // words2 == words1
    std::list<std::string> words2(words1.begin(), words1.end());
    std::cout << "2: " << words2;
    // words3 == words1
    std::list<std::string> words3(words1);
    std::cout << "3: " << words3;
    // words4 は {"Mo", "Mo", "Mo", "Mo", "Mo"}
    std::list<std::string> words4(5, "Mo");
    std::cout << "4: " << words4;
    const auto rg = {"cat", "cow", "crow"};
#ifdef __cpp_lib_containers_ranges
    std::list<std::string> words5(std::from_range, rg); // オーバーロード (6)
#else
    std::list<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 438 C++98 オーバーロード ( 5 ) InputIt が整数型の場合のみ
オーバーロード ( 4 ) を呼び出していた
オーバーロード ( 4 ) を呼び出すのは
InputIt LegacyInputIterator でない場合
LWG 2193 C++11 デフォルトコンストラクタがexplicitだった non-explicitに変更
LWG 2210 C++11 オーバーロード ( 3 ) にアロケータパラメータがなかった パラメータを追加
N3346 C++11 オーバーロード ( 3 ) において、コンテナ内の
要素はvalue-initializedされていた
default-insertedされる

関連項目

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