[RESOLVED] mem_fun & templates
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] mem_fun & templates

  1. #1
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,699

    [RESOLVED] mem_fun & templates

    This is the first time I've used std::mem_fun and I'm at a loss to figure out what I have to do to get this to compile.

    Code:
    #include <algorithm>
    #include <vector>
    #include <functional>
    
    using namespace std;
    
    template <typename T>
    class Test
    {
    public:
    
        void Function1(T &t)
        {
        }
    
        void Function2()
        {
            for_each(data.begin(), data.end(), mem_fun(&Test<T>::Function1)); // Error here.
        }
    
    private:
        vector<T> data;
    };
    
    int main()
    {
        Test<int> test;
    
        test.Function2();
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,699

    Re: mem_fun & templates

    The fact it is a template seems to be irrelevant.

    Code:
    #include <algorithm>
    #include <vector>
    #include <functional>
    
    using namespace std;
    
    class Test
    {
    public:
    
        void Function1(int t)
        {
        }
    
        void Function2()
        {
            for_each(data.begin(), data.end(), mem_fun(&Test::Function1));
        }
    
    private:
        vector<int> data;
    };
    
    int main()
    {
        Test test;
    
        test.Function2();
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  3. #3
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,699

    Re: mem_fun & templates

    Found the answer!
    Code:
    #include <algorithm>
    #include <vector>
    #include <functional>
    
    using namespace std;
    
    class Test
    {
    public:
    
        void Function1(int t)
        {
        }
    
        void Function2()
        {
            for_each(data.begin(), data.end(), bind1st(mem_fun(&Test::Function1), this));
        }
    
    private:
        vector<int> data;
    };
    
    int main()
    {
        Test test;
    
        test.Function2();
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  4. #4
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: [RESOLVED] mem_fun & templates

    Can you teach me how bind1st and bind2nd works because i read a lot of doc but never understand ?

    Thanks.
    Thanks for your help.

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,699

    Re: [RESOLVED] mem_fun & templates

    bind1st & bind2nd allow you to use a binary function (two parameters) where a unary function (one parameter) is expected.

    Take std::greater for example.
    This is a binary function, but it's often used where a unary function is expected, for example std::count_if.

    If you wanted to count how many items in a container were greater than 10 then you would use the following.
    Code:
    #include <functional>
    #include <vector>
    #include <algorithm>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
        vector<int> data;
    
        for (int i = 0; i < 100; ++i)
        {
            data.push_back(rand());
        }
    
        vector<int>::size_type count = count_if(data.begin(), data.end(), bind2nd(greater<int>(), 10));
    }
    bind2ndt makes the second argument to greater always equal to 10.
    The first argument is the value of the element in the vector.

    It's functionally equivalent to

    Code:
    #include <functional>
    #include <vector>
    #include <algorithm>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
        vector<int> data;
    
        for (int i = 0; i < 100; ++i)
        {
            data.push_back(rand());
        }
    
        greater<int> test;
    
        vector<int>::size_type count = 0;
        for (vector<int>::size_type i = 0; i < data.size(); ++i)
        {
            if (test(data[i], 10))
            {
                ++count;
            }
        }
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center