#include <list>
#include <functional>
#include <algorithm>
#include <iostream>
using namespace std;
typedef struct astruct {
astruct(int _a, int _b) : a(_a), b(_b){}
int a;
int b;
}astruct;template <class T>
class in_adver : public binary_function<T, int, bool>
{
public:
bool operator()(T it, int id)
{
return (it->a == id);
}
};typedef list<astruct> LIST;
int main(int argc, char* argv[])
{
LIST l;
for (int i = 0; i < 50; i++)
l.push_back(astruct(i, -i));

list<int> l2;
for (i = 0; i < 10; i++)
l2.push_back(i * 5);

list<int>::iterator it = l2.begin();
for ( ; it != l2.end(); ++it)
remove_if(l.begin(), l.end(), 
binder2nd<in_adver<LIST::iterator> >(
in_adver<LIST::iterator>(), *it));

LIST::iterator it2 = l.begin();
for ( ; it2 != l.end(); ++it2)
cout << "(" << it2->a << "," << it2->b << ")" << endl; return 0;
}

解决方案 »

  1.   

    上面的编译结果是:
    c:\program files\microsoft visual studio\vc98\include\algorithm(50) : error C2664: '()' : cannot convert parameter 1 from 'struct astruct' to 'const class std::list<struct astruct,class std::allocator<struct astruct> >::iterator &'
            Reason: cannot convert from 'struct astruct' to 'const class std::list<struct astruct,class std::allocator<struct astruct> >::iterator'
            No constructor could take the source type, or constructor overload resolution was ambiguous
            c:\program files\microsoft visual studio\vc98\include\algorithm(299) : see reference to function template instantiation 'class std::list<struct astruct,class std::allocator<struct astruct> >::iterator __cdecl std::find_if(class std::list<str
    uct astruct,class std::allocator<struct astruct> >::iterator,class std::list<struct astruct,class std::allocator<struct astruct> >::iterator,class std::binder2nd<class in_adver<class std::list<struct astruct,class std::allocator<struct astruct> >::i
    terator> >)' being compiled
    c:\program files\microsoft visual studio\vc98\include\algorithm(50) : error C2064: term does not evaluate to a function
            c:\program files\microsoft visual studio\vc98\include\algorithm(299) : see reference to function template instantiation 'class std::list<struct astruct,class std::allocator<struct astruct> >::iterator __cdecl std::find_if(class std::list<str
    uct astruct,class std::allocator<struct astruct> >::iterator,class std::list<struct astruct,class std::allocator<struct astruct> >::iterator,class std::binder2nd<class in_adver<class std::list<struct astruct,class std::allocator<struct astruct> >::i
    terator> >)' being compiled
    c:\program files\microsoft visual studio\vc98\include\algorithm(316) : error C2664: '()' : cannot convert parameter 1 from 'struct astruct' to 'const class std::list<struct astruct,class std::allocator<struct astruct> >::iterator &'
            Reason: cannot convert from 'struct astruct' to 'const class std::list<struct astruct,class std::allocator<struct astruct> >::iterator'
            No constructor could take the source type, or constructor overload resolution was ambiguous
            c:\program files\microsoft visual studio\vc98\include\algorithm(304) : see reference to function template instantiation 'class std::list<struct astruct,class std::allocator<struct astruct> >::iterator __cdecl std::remove_copy_if(class std::l
    ist<struct astruct,class std::allocator<struct astruct> >::iterator,class std::list<struct astruct,class std::allocator<struct astruct> >::iterator,class std::list<struct astruct,class std::allocator<struct astruct> >::iterator,class std::binder2nd<
    class in_adver<class std::list<struct astruct,class std::allocator<struct astruct> >::iterator> >)' being compiled
    c:\program files\microsoft visual studio\vc98\include\algorithm(316) : error C2064: term does not evaluate to a function
            c:\program files\microsoft visual studio\vc98\include\algorithm(304) : see reference to function template instantiation 'class std::list<struct astruct,class std::allocator<struct astruct> >::iterator __cdecl std::remove_copy_if(class std::l
    ist<struct astruct,class std::allocator<struct astruct> >::iterator,class std::list<struct astruct,class std::allocator<struct astruct> >::iterator,class std::list<struct astruct,class std::allocator<struct astruct> >::iterator,class std::binder2nd<
    class in_adver<class std::list<struct astruct,class std::allocator<struct astruct> >::iterator> >)' being compiled
    Error executing cl.exe.taa.exe - 4 error(s), 0 warning(s)
      

  2.   

    msdn 中 copy 出来的// functional_bind2nd.cpp
    // compile with: /EHsc
    #include <vector>
    #include <functional>
    #include <algorithm>
    #include <iostream>using namespace std;// Creation of a user-defined function object 
    // that inherits from the unary_function base class
    class greaterthan15: unary_function<int, bool> 
    {
    public:
       result_type operator( ) ( argument_type i )
       {
          return ( result_type ) ( i > 15 );
       }
    };int main( )
    {
       vector <int> v1;
       vector <int>::iterator Iter;
       
       int i;
       for ( i = 0 ; i <= 5 ; i++ )
       {
          v1.push_back( 5 * i );
       }   cout << "The vector v1 = ( " ;
       for ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ )
          cout << *Iter << " ";
       cout << ")" << endl;   // Count the number of integers > 10 in the vector
       int result1a;
       result1a = count_if( v1.begin( ), v1.end( ), bind2nd( greater<int>( ), 10 ) );
       cout << "The number of elements in v1 greater than 10 is: "
            << result1a << "." << endl;   // Compare counting the number of integers > 15 in the vector
       // with a user-defined function object
       int result1b;
       result1b = count_if( v1.begin( ), v1.end( ), greaterthan15( ) );
       cout << "The number of elements in v1 greater than 15 is: "
            << result1b << "." << endl;   // Count the number of integers < 10 in the vector
       int result2;
       result2 = count_if( v1.begin( ), v1.end( ), bind1st( greater<int>( ), 10 ) );
       cout << "The number of elements in v1 less than 10 is: "
            << result2 << "." << endl;
    }
      

  3.   

    但我用的是binary_function呀,不是一个参数的