Thread: bind on member functions of stl containers

  1. #1
    Registered User
    Join Date
    Jun 2008
    Tokyo, Japan

    bind on member functions of stl containers


    I would like to accomplish
    #include <tr1/functional>
    #include <iostream>
    #include <vector>
    #include <set>
    #include <algorithm>
    using namespace std;
    using namespace tr1;
    using namespace placeholders;
    int main()
    	int ar[] = {0, 1, 2, 3, 4, 5};
    	vector<int> ivec(ar, ar+6);
    	set<int> iset(ar, ar+6);
    	for_each(ivec.begin(), ivec.end(),
    			bind(&std::set<int>::erase, &iset, _1));
    	cout << iset.size();
    	return 0;
    and I do not see why this piece of code does not compile...

  2. #2
    The larch
    Join Date
    May 2006
    The reason is (assuming there is this tr1/functional header with all the stuff, I used boost::lambda) that set::erase is an overloaded method. You can't deduce template arguments from an overloaded function unless you are more explicit.

    (With boost) I did this:
    set<int>::size_type( set<int>::*mp)(const int&) = &set<int>::erase;
    	for_each(ivec.begin(), ivec.end(),
    			bind(mp, &iset, _1));
    It should also work with a static cast of the function pointer.

    As you can see it is rather verbous. Whether it pays off is up to you.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jun 2008
    Tokyo, Japan
    Thank you! I did not think about it and pointers to member functions are not trivial... I'll use this solution with a comment and a good name.

    std::tr1::bind is equivalent to boost::bind, it allows to not depend on boost as it is now part of most C++ implementations. TR1 also has shared_ptr and ref, cref (and other things I am not using.)

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    Although, I'd recommend going for boost for now.
    Not all compilers have TR1 implementations.
    On the other hand, you can just use boost and it works everywhere.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes and member functions
    By uraliss in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2006, 07:38 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. Pointer Elements & STL Containers :: STL
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2002, 08:13 PM