Condividi tramite


Usare le funzioni list::remove, list::remove_if STL in Visual C++

Questo articolo fornisce informazioni su come usare le list::removefunzioni STL list::remove_if in Visual C++.

Versione originale del prodotto: Visual C++
Numero KB originale: 168047

Riepilogo

Il codice di esempio seguente illustra come usare le list::removefunzioni STL list::remove_if in Visual C++.

Note

Esistono alcune differenze nell'implementazione dei componenti della libreria C++ Standard in Visual C++ versione 4.2 rispetto alle revisioni successive. Le sezioni pertinenti del codice riportato di seguito vengono compilate in modo condizionale in base al valore di _MSC_VER.

Intestazione obbligatoria

<list>
<string>
<iostream>

Prototipo

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

Note

I nomi di classe/parametro nel prototipo potrebbero non corrispondere alla versione nel file di intestazione. Alcuni sono stati modificati per migliorare la leggibilità.

Descrizione

In questo esempio viene illustrato come usare list::remove e list::remove_if. Illustra anche come usare list::remove_if con la propria funzione.

Codice di esempio

//////////////////////////////////////////////////////////////////////
// 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";
}

Output del programma

good bad ugly
good ugly
good
Empty list

Riferimenti

Per altre informazioni su list::remove e list::remove_if, visitare i siti Web seguenti: