list::assign (STL Samples)

在 Visual C++ 演示如何使用 列表:: 分配 标准 (STL)模板库函数。

void assign(
   const_iterator First,
   const_iterator Last
);
void assign(
   size_type n,
   const T& x = T( )
);
iterator erase(
   iterator It
);
iterator erase(
   iterator First,
   iterator Last
); bool empty( ) const;

备注

备注

类/参数名在原型不匹配版本在头文件。修改某些提高可读性。

第一个成员函数替换序列控件 * 与该序列 [First, Last) 的 。 第二个成员函数替换序列控件 * 使用值 X. 的n 元素重复*。* 第三个成员函数中移除该控件序列的元素指向由 。 第四个成员函数移除控件序列的元素在范围 [First, Last)。 两个返回指定保持在所有元素外的第一个元素中移除的迭代器,或者 结束 ,如果不存在这样的元素。 最后一个成员函数返回一个空控件序列的 true

示例

// assign.cpp 
// compile with: /EHsc
//
// Shows various ways to assign and erase elements
//        from a list<T>.
//
// Functions:
//    list::assign
//    list::empty
//    list::erase

#include <list>
#include <iostream>

using namespace std ;

typedef list<int> LISTINT;

int main()
{
    LISTINT listOne;
    LISTINT listAnother;
    LISTINT::iterator i;

    // Add some data
    listOne.push_front (2);
    listOne.push_front (1);
    listOne.push_back (3);

    listAnother.push_front(4);

    listAnother.assign(listOne.begin(), listOne.end());

    // 1 2 3
    for (i = listAnother.begin(); i != listAnother.end(); ++i)
        cout << *i << " ";
    cout << endl;

    listAnother.assign(4, 1);

    // 1 1 1 1
    for (i = listAnother.begin(); i != listAnother.end(); ++i)
        cout << *i << " ";
    cout << endl;

    listAnother.erase(listAnother.begin());

    // 1 1 1
    for (i = listAnother.begin(); i != listAnother.end(); ++i)
        cout << *i << " ";
    cout << endl;

    listAnother.erase(listAnother.begin(), listAnother.end());
    if (listAnother.empty())
        cout << "All gone\n";
}

Output

1 2 3 
1 1 1 1 
1 1 1 
All gone

要求

**标题:**list

请参见

概念

标准模板库示例