共用方式為


list::assign

Erases elements from a list and copies a new set of elements to a target list.

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

Parameters

  • First
    Position of the first element in the range of elements to be copied from the argument list.

  • Last
    Position of the first element just beyond the range of elements to be copied from the argument list.

  • Count
    The number of copies of an element being inserted into the list.

  • Val
    The value of the element being inserted into the list.

  • IList
    The initializer_list that contains the elements to be inserted.

Remarks

After erasing any existing elements in the target list, assign either inserts a specified range of elements from the original list or from some other list into the target list or inserts copies of a new element of a specified value into the target list

Example

// 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;
}
c1 = 10 20 30
c1 = 50 60
c1 = 4 4 4 4 4 4 4
c1 = 10 20 30 40

Requirements

Header: <list>

Namespace: std

See Also

Reference

list Class

Standard Template Library