Delen via


De functies list::remove, list::remove_if STL gebruiken in Visual C++

In dit artikel vindt u informatie over het gebruik van de list::removelist::remove_if STL-functies in Visual C++.

Oorspronkelijke productversie: Visual C++
Oorspronkelijk KB-nummer: 168047

Samenvatting

In de onderstaande voorbeeldcode ziet u hoe u de list::removeSTL-functie list::remove_if (s) gebruikt in Visual C++.

Notitie

Er zijn enkele verschillen in de implementatie van de onderdelen van de Standard C++-bibliotheek in Visual C++ versie 4.2 versus latere revisies. De relevante secties van de onderstaande code worden voorwaardelijk gecompileerd op basis van de waarde van _MSC_VER.

Vereiste header

<list>
<string>
<iostream>

Prototype

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

Notitie

De namen van klassen/parameters in het prototype komen mogelijk niet overeen met de versie in het headerbestand. Sommige zijn gewijzigd om de leesbaarheid te verbeteren.

Beschrijving

In dit voorbeeld ziet u hoe u deze kunt gebruiken list::remove en list::remove_if. Ook ziet u hoe u deze kunt gebruiken list::remove_if met uw eigen functie.

Voorbeeldcode

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

Programma-uitvoer

good bad ugly
good ugly
good
Empty list

Verwijzingen

Ga voor meer informatie over list::remove en list::remove_ifga naar de volgende websites: