共用方式為


list::list

建構特定大小的清單,或具有特定值之元素的清單,或具有特定配置器的清單,或是做為其他一些清單的所有或部分複本。

list( ); explicit list(     const Allocator& Al ); explicit list(     size_type Count ); list(     size_type Count,     const Type& Val ); list(     size_type Count,     const Type& Val,     const Allocator& Al ); list(     const list& Right ); list(     list&& Right ); list(     initializer_list<Type> IList,     const Allocator& Al ); template<class InputIterator>     list(         InputIterator First,         InputIterator Last     ); template<class InputIterator >     list(         InputIterator First,         InputIterator Last,         const Allocator& Al     ); 

參數

參數

描述

Al

搭配這個物件使用的配置器類別。

Count

建構的清單中元素的數目。

Val

list 中元素的值。

Right

list,其中有要複製的建構的 list。

First

要複製的元素範圍中第一個元素的位置。

Last

超出要複製之元素範圍的第一個元素的位置。

IList

包含要複製之項目的 initializer_list。

備註

所有的建構函式都會儲存配置器物件 (Al) 並初始化 list。

get_allocator 傳回用來建構清單的配置器物件的複本。

前兩個建構函式指定空的初始 list,第二個指定要用的配置器類型 (Al)。

第三個建構函式指定類別 Type 之預設值的指定元素數 (Count) 的重複。

第四及第五個建構函式指定 Val 值的 (Count) 元素數的重複。

第六個建構函式指定清單 Right 的複本。

第七個建構函式將清單移至 Right。

第八個建構函式使用 initializer_list 來指定元素。

下面兩個建構函式複製清單的範圍 [First, Last)。

沒有建構函式會執行任何暫時重新配置。

範例

// list_class_list.cpp
// compile with: /EHsc
#include <list>
#include <iostream>

int main()
{
    using namespace std;
    // Create an empty list c0
    list <int> c0;

    // Create a list c1 with 3 elements of default value 0
    list <int> c1(3);

    // Create a list c2 with 5 elements of value 2
    list <int> c2(5, 2);

    // Create a list c3 with 3 elements of value 1 and with the 
    // allocator of list c2
    list <int> c3(3, 1, c2.get_allocator());

    // Create a copy, list c4, of list c2
    list <int> c4(c2);

    // Create a list c5 by copying the range c4[_First, _Last)
    list <int>::iterator c4_Iter = c4.begin();
    c4_Iter++;
    c4_Iter++;
    list <int> c5(c4.begin(), c4_Iter);

    // Create a list c6 by copying the range c4[_First, _Last) and with 
    // the allocator of list c2
    c4_Iter = c4.begin();
    c4_Iter++;
    c4_Iter++;
    c4_Iter++;
    list <int> c6(c4.begin(), c4_Iter, c2.get_allocator());

    cout << "c1 =";
    for (auto c : c1)
        cout << " " << c;
    cout << endl;

    cout << "c2 =";
    for (auto c : c2)
        cout << " " << c;
    cout << endl;

    cout << "c3 =";
    for (auto c : c3)
        cout << " " << c;
    cout << endl;

    cout << "c4 =";
    for (auto c : c4)
        cout << " " << c;
    cout << endl;

    cout << "c5 =";
    for (auto c : c5)
        cout << " " << c;
    cout << endl;

    cout << "c6 =";
    for (auto c : c6)
        cout << " " << c;
    cout << endl;

    // Move list c6 to list c7
    list <int> c7(move(c6));
    cout << "c7 =";
    for (auto c : c7)
        cout << " " << c;
    cout << endl;

    // Construct with initializer_list
    list<int> c8({ 1, 2, 3, 4 });
    cout << "c8 =";
    for (auto c : c8)
        cout << " " << c;
    cout << endl;
}
  

需求

標頭:<list>

命名空間: std

請參閱

參考

list 類別

標準樣板程式庫