Compartir a través de


Use las funciones de STL list::remove, list::remove_if en Visual C++

En este artículo se proporciona información sobre cómo usar las list::removefunciones de , list::remove_if STL en Visual C++.

Versión original del producto: Visual C++
Número de KB original: 168047

Resumen

En el código de ejemplo siguiente se muestra cómo usar las list::removefunciones de , list::remove_if STL en Visual C++.

Nota:

Hay algunas diferencias en la implementación de los componentes de la biblioteca estándar de C++ en visual C++ versión 4.2 frente a revisiones posteriores. Las secciones pertinentes del código siguiente se compilan condicionalmente en función del valor de _MSC_VER.

Encabezado obligatorio

<list>
<string>
<iostream>

Prototipo

void remove(const T& x);
void remove_if(binder2nd< not_equal_to<T> > pr);

Nota:

Es posible que los nombres de clase o parámetro del prototipo no coincidan con la versión del archivo de encabezado. Algunas se han modificado para mejorar la legibilidad.

Descripción

En este ejemplo se muestra cómo usar list::remove y list::remove_if. También muestra cómo usar list::remove_if con su propia función.

Código de ejemplo

//////////////////////////////////////////////////////////////////////
// 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
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////

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

#include <list>
#include <string>
#include <iostream>
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<string> > LISTSTR;

// Used to customize list::remove_if()
class is_four_chars
    : public not_equal_to<string>
{
    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<string> >
        (not_equal_to<string>(), "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<string> >
        (is_four_chars(), "useless parameter"));

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

Salida del programa

good bad ugly
good ugly
good
Empty list

Referencias

Para obtener más información sobre list::remove y list::remove_if, visite los siguientes sitios web: