Article ID: 158085
Article Last Modified on 8/11/2005
<list>
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;NOTE: The class/parameter names in the prototype may not match the version in the header file. Some have been modified to improve readability.
//////////////////////////////////////////////////////////////////////
//
// Compile options needed: -GX
//
// assign.cpp : Shows the various ways to assign and erase elements
// from a list<T>.
//
// Functions:
//
// list::assign
// list::empty
// list::erase
//
// Written by Andrew Bradnan
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////
#include <list>
#include <iostream>
#if _MSC_VER > 1020 // if VC++ version is > 4.2
using namespace std; // std c++ libs implemented in std
#endif
typedef list<int, allocator<int> > LISTINT;
void 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";
}
Program Output is: 1 2 3 1 1 1 1 1 1 1 All gone
Additional query words: STL STLSample list::assign list::empty list::erase
Keywords: kbhowto kbinfo kbtemplate kbstl kbcode kbfunctions KB158085