名前空間
変種
操作

std::list

提供: cppreference.com
< cpp‎ | container
 
 
 
 
ヘッダ <list> で定義
template<

    class T,
    class Allocator = std::allocator<T>

> class list;
(1)
namespace pmr {

    template <class T>
    using list = std::list<T, std::pmr::polymorphic_allocator<T>>;

}
(2) (C++17以上)

std::list はコンテナ内の任意の位置への要素の定数時間の挿入と削除をサポートするコンテナです。 高速なランダムアクセスはサポートされません。 これは通常、双方向連結リストとして実装されます。 std::forward_list と比較すると、このコンテナは双方向のイテレーション能力を提供しますが、空間効率は悪くなります。

リスト内の要素の追加、削除、移動、およびいくつかのリストを横断するそれらの操作は、イテレータや参照を無効化しません。 イテレータは対応する要素が削除される場合にのみ無効化されます。

std::listContainer, AllocatorAwareContainer, SequenceContainer, ReversibleContainer の要件を満たします。

目次

[編集] テンプレート引数

T - 要素の型。
TCopyAssignable および CopyConstructible の要件を満たさなければなりません。 (C++11未満)
要素に課される要件はコンテナに対して実際に行われる操作によります。 一般的には、要素型は完全型であることが要求され、 Erasable の要件を満たさなければなりませんが、使用するメンバ関数によってはさらに厳しい要件が課されます。 (C++11以上)
(C++17未満)
要素に課される要件はコンテナに対して実際に行われる操作によります。 一般的には、要素型は Erasable の要件を満たさなければなりませんが、使用するメンバ関数によってはさらに厳しい要件が課されます。 アロケータがアロケータの完全性の要件を満たす場合、要素型が不完全でもこのコンテナを実体化できます (しかしそのメンバはできません)。 (C++17以上)

[edit]

Allocator - メモリを確保/解放したり、そのメモリに要素を構築/破棄したりするために使用されるアロケータ。 この型は Allocator の要件を満たさなければなりません。 Allocator::value_typeT が同じでない場合、動作は未定義です。 [edit]

[編集] メンバ型

メンバ型 定義
value_type T [edit]
allocator_type Allocator [edit]
size_type 符号なし整数型 (通常 std::size_t) [edit]
difference_type 符号付き整数型 (通常 std::ptrdiff_t) [edit]
reference
Allocator::reference (C++11未満)
value_type& (C++11以上)
[edit]
const_reference
Allocator::const_reference (C++11未満)
const value_type& (C++11以上)
[edit]
pointer
Allocator::pointer (C++11未満)
std::allocator_traits<Allocator>::pointer (C++11以上)
[edit]
const_pointer
Allocator::const_pointer (C++11未満)
std::allocator_traits<Allocator>::const_pointer (C++11以上)
[edit]
iterator LegacyBidirectionalIterator [edit]
const_iterator const LegacyBidirectionalIterator [edit]
reverse_iterator std::reverse_iterator<iterator> [edit]
const_reverse_iterator std::reverse_iterator<const_iterator> [edit]

[編集] メンバ関数

list を構築します
(パブリックメンバ関数) [edit]
list を破棄します
(パブリックメンバ関数) [edit]
コンテナに値を代入します
(パブリックメンバ関数) [edit]
コンテナに値を代入します
(パブリックメンバ関数) [edit]
関連付けられているアロケータを返します
(パブリックメンバ関数) [edit]
要素アクセス
最初の要素にアクセスします
(パブリックメンバ関数) [edit]
最後の要素にアクセスします
(パブリックメンバ関数) [edit]
イテレータ
先頭を指すイテレータを返します
(パブリックメンバ関数) [edit]
終端を指すイテレータを返します
(パブリックメンバ関数) [edit]
先頭を指す逆イテレータを返します
(パブリックメンバ関数) [edit]
終端を指す逆イテレータを返します
(パブリックメンバ関数) [edit]
容量
コンテナが空かどうか調べます
(パブリックメンバ関数) [edit]
要素数を返します
(パブリックメンバ関数) [edit]
可能な最大の要素数を返します
(パブリックメンバ関数) [edit]
変更
すべての要素を削除します
(パブリックメンバ関数) [edit]
要素を挿入します
(パブリックメンバ関数) [edit]
(C++11)
要素をその場で構築します
(パブリックメンバ関数) [edit]
要素を削除します
(パブリックメンバ関数) [edit]
要素を末尾に追加します
(パブリックメンバ関数) [edit]
要素を末尾にその場で構築します
(パブリックメンバ関数) [edit]
最後の要素を削除します
(パブリックメンバ関数) [edit]
要素を先頭に挿入します
(パブリックメンバ関数) [edit]
要素を先頭にその場で構築します
(パブリックメンバ関数) [edit]
最初の要素を削除します
(パブリックメンバ関数) [edit]
格納されている要素の数を変更します
(パブリックメンバ関数) [edit]
(C++11)
内容を入れ替えます
(パブリックメンバ関数) [edit]
操作
ソートされた2つのリストをマージします
(パブリックメンバ関数) [edit]
別の list から要素を移動します
(パブリックメンバ関数) [edit]
特定の基準を満たす要素を削除します
(パブリックメンバ関数) [edit]
要素の順序を反転します
(パブリックメンバ関数) [edit]
連続した重複要素を削除します
(パブリックメンバ関数) [edit]
要素をソートします
(パブリックメンバ関数) [edit]

[編集] 非メンバ関数

(C++20で削除)(C++20で削除)(C++20で削除)(C++20で削除)(C++20で削除)(C++20)
list 内の値を辞書的に比較します
(関数テンプレート) [edit]
std::swap アルゴリズムの特殊化
(関数テンプレート) [edit]
特定の基準を満たすすべての要素を削除します
(関数テンプレート) [edit]

[編集] 推定ガイド(C++17以上)

[編集]

#include <algorithm>
#include <iostream>
#include <list>
 
int main()
{
    // Create a list containing integers
    std::list<int> l = { 7, 5, 16, 8 };
 
    // Add an integer to the front of the list
    l.push_front(25);
    // Add an integer to the back of the list
    l.push_back(13);
 
    // Insert an integer before 16 by searching
    auto it = std::find(l.begin(), l.end(), 16);
    if (it != l.end()) {
        l.insert(it, 42);
    }
 
    // Iterate and print values of the list
    for (int n : l) {
        std::cout << n << '\n';
    }
}

出力:

25
7
5
42
16
8
13