Compartilhar via


Usar as funções STL list::remove, list::remove_if no Visual C++

Este artigo fornece informações sobre como usar as list::removefunções STL no list::remove_if Visual C++.

Versão original do produto: Visual C++
Número original do KB: 168047

Resumo

O código de exemplo a seguir ilustra como usar as list::removefunções STL no list::remove_if Visual C++.

Observação

Há algumas diferenças na implementação dos componentes da Biblioteca C++ Padrão no Visual C++ versão 4.2 em comparação com as revisões posteriores. As seções relevantes do código abaixo são compiladas condicionalmente com base no valor de _MSC_VER.

Cabeçalho necessário

<list>
<string>
<iostream>

Protótipo

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

Observação

Os nomes de classe/parâmetro no protótipo podem não corresponder à versão no arquivo de cabeçalho. Alguns foram modificados para melhorar a legibilidade.

Descrição

Este exemplo mostra como usar list::remove e list::remove_if. Ele também mostra como usar list::remove_if com sua própria função.

Código de exemplo

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

Saída do programa

good bad ugly
good ugly
good
Empty list

Referências

Para obter mais informações sobre list::remove e list::remove_if, visite os seguintes sites: