Namespaces
Variants

std::forward_list<T,Allocator>:: forward_list

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

forward_list ( InputIt first, InputIt last,

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

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

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

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

1) デフォルトコンストラクタ。デフォルト構築されたアロケータを持つ空の forward_list を構築します。
Allocator DefaultConstructible でない場合、動作は未定義です。
2) 指定されたアロケータ alloc で空の forward_list を構築します。
3) forward_list count 個のデフォルト構築された T オブジェクトで構築します。コピーは作成されません。
T DefaultInsertable でない場合、 forward_list への挿入時の動作は未定義です。
4) forward_list count 個の value 値を持つ要素で構築します。
T CopyInsertable でない場合、 forward_list への挿入時の動作は未定義です。
5) 範囲 [ first , last ) の内容を持つ forward_list を構築します。 [ first , last ) 内の各イテレータは正確に1回だけデリファレンスされます。
このオーバーロードは、 InputIt LegacyInputIterator の要件を満たす場合にのみ、オーバーロード解決に参加します。
T EmplaceConstructible でなく、 forward_list * first から構築できない場合、動作は未定義です。
6) 範囲 rg の内容から forward_list を構築します。 rg 内の各イテレータは正確に一度だけデリファレンスされます。
Tが forward_list EmplaceConstructible でない場合、 * ranges:: begin ( rg ) からの構築は未定義動作となります。
7) コピーコンストラクタ。 forward_list other の内容で構築します。アロケータは std:: allocator_traits < Allocator > :: select_on_container_copy_construction
( other. get_allocator ( ) )
を呼び出すことによって取得されます。
8) ムーブコンストラクタ。 forward_list other の内容で構築する。アロケータは other. get_allocator ( ) からのムーブ構築によって取得される。
9) コピーコンストラクタと同様ですが、 alloc がアロケータとして使用される点が異なります。
T CopyInsertable でない場合、 forward_list への挿入は未定義動作となります。
10) ムーブコンストラクタと同様ですが、 alloc がアロケータとして使用される点が異なります。
T MoveInsertable でない場合、動作は未定義です。
11) 次と等価: forward_list ( il. begin ( ) , il. end ( ) , alloc )

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - C++関連の専門用語(Parameters, Complexity, Exceptions, Notes, Example, Defect reports, See also)は原文のまま保持しました - HTMLタグ、属性、クラス名、IDなどは一切変更していません - 数値や記号類もそのまま保持しています - フォーマットと構造は完全に維持しています

パラメータ

alloc - このコンテナのすべてのメモリ割り当てに使用するアロケータ
count - コンテナのサイズ
value - コンテナの要素を初期化するための値
first, last - コピーする要素のソース 範囲 を定義するイテレータのペア
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) Ranges-aware 構築と挿入; オーバーロード ( 6 )

#include <forward_list>
#include <iostream>
#include <string>
template<typename T>
std::ostream& operator<<(std::ostream& s, const std::forward_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::forward_list<std::string> words1{"the", "frogurt", "is", "also", "cursed"};
    std::cout << "1: " << words1;
    // words2 == words1
    std::forward_list<std::string> words2(words1.begin(), words1.end());
    std::cout << "2: " << words2;
    // words3 == words1
    std::forward_list<std::string> words3(words1);
    std::cout << "3: " << words3;
    // words4 は {"Mo", "Mo", "Mo", "Mo", "Mo"}
    std::forward_list<std::string> words4(5, "Mo");
    std::cout << "4: " << words4;
    const auto rg = {"cat", "cow", "crow"};
#ifdef __cpp_lib_containers_ranges
    std::forward_list<std::string> words5(std::from_range, rg); // オーバーロード (6)
#else
    std::forward_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 2193 C++11 デフォルトコンストラクタが explicit であった non-explicit に変更
LWG 2210 C++11 オーバーロード ( 3 ) にアロケータパラメータがなかった パラメータを追加
N3346 C++11 オーバーロード ( 3 ) において、コンテナ内の要素は
値初期化されていた
デフォルト挿入されるように変更

関連項目

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