Notes
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de vous connecter ou de modifier des répertoires.
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de modifier des répertoires.
Cet article fournit des informations sur l’utilisation des list::remove
fonctions STL list::remove_if
dans Visual C++.
Version du produit d’origine : Visual C++
Numéro de base de connaissances d’origine : 168047
Résumé
L’exemple de code ci-dessous montre comment utiliser la list::remove
list::remove_if
ou les fonctions STL dans Visual C++.
Note
Il existe des différences dans l’implémentation des composants de la bibliothèque C++ Standard dans Visual C++ version 4.2 par rapport aux révisions ultérieures. Les sections pertinentes du code ci-dessous sont compilées de manière conditionnelle en fonction de la valeur de _MSC_VER
.
En-tête requis
<list>
<string>
<iostream>
Prototype
void remove(const T& x);
void remove_if(binder2nd< not_equal_to<T> > pr);
Note
Les noms de classe/paramètre dans le prototype peuvent ne pas correspondre à la version dans le fichier d’en-tête. Certains ont été modifiés pour améliorer la lisibilité.
Description
Cet exemple montre comment utiliser list::remove
et list::remove_if
. Il montre également comment utiliser list::remove_if
avec votre propre fonction.
Exemple de 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
// 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";
}
Sortie du programme
good bad ugly
good ugly
good
Empty list
References
Pour plus d’informations sur et list::remove_if
pour plus d’informationslist::remove
, visitez les sites web suivants :