Help and Support
 

powered byLive Search

How to use the list::assign, list::empty, and list::erase STL functions in Visual C++

Article ID:158085
Last Review:August 11, 2005
Revision:5.0
This article was previously published under Q158085
NOTE: Microsoft Visual C++ .NET (2002) and Visual C++ .NET (2003) support both the managed code model that is provided by the .NET Framework and the unmanaged native Windows code model. The information in this article applies to unmanaged Visual C++ code only.
On This Page

SUMMARY

The sample code below illustrates how to use the list::assign, list::empty, and list::erase STL functions in Visual C++.

Back to the top

MORE INFORMATION

Required Header

   <list>
				

Back to the top

Prototype

   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.

Back to the top

Description

The first member function replaces the sequence controlled by *this with the sequence [first, last). The second member function replaces the sequence controlled by *this with a repetition of n elements of value x.

The third member function removes the element of the controlled sequence pointed to by it. The fourth member function removes the elements of the controlled sequence in the range [first, last). Both return an iterator that designates the first element remaining beyond any elements removed, or end() if no such element exists.

The last member function returns true for an empty controlled sequence.

Back to the top

Sample Code

////////////////////////////////////////////////////////////////////// 
// 
// 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
				

Back to the top

REFERENCES

For the same information about list::assign, list::empty, and list::erase, visit the following MSDN Web site:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vcsampsamplelistccassignstlsample.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vcsampsamplelistccassignstlsample.asp)

Back to the top


APPLIES TO
The Standard C++ Library, when used with:
  Microsoft Visual C++ 4.2 Enterprise Edition
  Microsoft Visual C++ 5.0 Enterprise Edition
  Microsoft Visual C++ 6.0 Enterprise Edition
  Microsoft Visual C++ 4.2 Professional Edition
  Microsoft Visual C++ 5.0 Professional Edition
  Microsoft Visual C++ 6.0 Professional Edition
  Microsoft Visual C++ 6.0 Standard Edition
  Microsoft Visual C++ .NET 2002 Standard Edition
  Microsoft Visual C++ .NET 2003 Standard Edition

Back to the top

Keywords: 
kbhowto kbinfo kbtemplate kbstl kbcode kbfunctions KB158085

Back to the top

Article Translations

 

Other Support Options

  • Need More Help?
    Contact a Support professional by Email, Online or Phone.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.