list::assign

清除列表中的元素,并将一组新元素复制到目标列表。

void assign(     size_type Count,     const Type& Val ); void assign     initializer_list<Type> IList ); template<class InputIterator>     void assign(         InputIterator First,         InputIterator Last     ); 

参数

  • First
    要从参数列表中复制的一系列元素中的第一个元素的位置。

  • Last
    超出要从参数列表中复制的一系列元素的范围的第一个元素的位置。

  • Count
    要插入列表中的元素副本的数目。

  • Val
    要插入到列表中的元素的值。

  • IList
    包含要插入的元素的 initializer_list。

备注

清除目标列表中的任何现有元素后,将原始列表或其他列表中的一系列指定的元素插入目标列表中,或将指定值的新元素的副本插入目标列表中

示例

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

int main()
{
    using namespace std;
    list<int> c1, c2;
    list<int>::const_iterator cIter;

    c1.push_back(10);
    c1.push_back(20);
    c1.push_back(30);
    c2.push_back(40);
    c2.push_back(50);
    c2.push_back(60);

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

    c1.assign(++c2.begin(), c2.end());
    cout << "c1 =";
    for (auto c : c1)
        cout << " " << c;
    cout << endl;

    c1.assign(7, 4);
    cout << "c1 =";
    for (auto c : c1)
        cout << " " << c;
    cout << endl;

    c1.assign({ 10, 20, 30, 40 });
    cout << "c1 =";
    for (auto c : c1)
        cout << " " << c;
    cout << endl;
}
  

要求

标头:<list>

命名空间: std

请参见

参考

list 类

标准模板库