Microsoft KB Archive/168047

= How to use the [list::remove, list::remove_if] STL function(s) in Visual C++ =

Article ID: 168047

Article Last Modified on 7/15/2005

-

APPLIES TO

 The Standard C++ Library, when used with:  Microsoft Visual C++ 4.2 Enterprise Edition

 Microsoft Visual C++ 4.2 Professional Edition

 Microsoft Visual C++ 5.0 Enterprise Edition

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q168047

<div class="summary_section">

SUMMARY
The sample code below illustrates how to use the [list::remove, list::remove_if] STL function(s) in Visual C++.

Note There are some differences in the implementation of the Standard C++ Library components in Visual C++ version 4.2 versus later revisions. The relevant sections of code below compile conditionally based upon the value of _MSC_VER.

<div class="moreinformation_section">

Prototype
void remove(const T& x); void remove_if(binder2nd< not_equal_to<T> > pr); Note The class/parameter names in the prototype may not match the version in the header file. Some have been modified to improve readability.

Description
This example shows how to use list::remove and list::remove_if. It also shows how to use list::remove_if with your own function.

Sample code
//////////////////////////////////////////////////////////////////////   //    // Compile options needed: -GX //   // remove.cpp :  This example shows how to use list::remove and //              list::remove_if. It also shows how to use //              list::remove_if with your own function. //   // Functions: //   //  list::remove // list::remove_if //   // Written by Andrew Bradnan // Copyright (c) 1996 Microsoft Corporation. All rights reserved. //////////////////////////////////////////////////////////////////////

#pragma warning(disable:4786) // disable spurious C4786 warnings

#include #include #include using namespace std;

#if _MSC_VER > 1020  // if later than revision 4.2 using namespace std;  // std c++ libs are implemented in std #endif

typedef list<string, allocator > LISTSTR;

// Used to customize list::remove_if class is_four_chars : public not_equal_to {     bool operator(const string& rhs, const string&) const { return rhs.size == 4; } };

void main {     LISTSTR test; LISTSTR::iterator i;

test.push_back("good"); test.push_back("bad"); test.push_back("ugly");

// good bad ugly for (i = test.begin; i != test.end; ++i) cout << *i << " "; cout << endl;

test.remove("bad");

// good ugly for (i = test.begin; i != test.end; ++i) cout << *i << " "; cout << endl;

// remove any not equal to "good" test.remove_if(binder2nd<not_equal_to >        (not_equal_to, "good"));

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

// Remove any strings that are four characters long test.remove_if(binder2nd<not_equal_to >        (is_four_chars, "useless parameter"));

if (test.empty) cout << "Empty list\n";

}

Program output
<pre class="fixed_text">  good bad ugly good ugly good Empty list

<div class="references_section">