remove_if函数的使用【转】

//
   //
   // 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
   //
   // Written by Andrew Bradnan
   // 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";

   }

http://support.microsoft.com/kb/168047/zh-tw

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值