Thread: How to pass member functions into a function object...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    How to pass member functions into a function object...

    C++ has many algorithms in the form:
    Code:
    algo_if ( iterator, iterator, unary_function_object );
    Take, for example:
    Code:
    algo_if (myContainer.begin(), myContainer.end(), bind1st (equal_to<size_t> (), 7))
    This is all well and good if myContainer contains ints, but what if it contained strings?

    equal_to, or perhaps algo_if, implicitly passes in myContainer.begin() as the first element for comparison, then myContainer.begin()+1, etc.

    What if I didn't want to pass in the element of a container...what if I wanted to pass in the string length of each element?!

    I have at my disposal strlen, which takes a string argument...
    and length, which takes no argument other than the implicit this.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can write your own function object to compare the string lengths, though you still have to pass in elements of the container.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    But how do I "grab at" this implicit iterator that has no name? I can't just say myIter->length() or strlen(*myIter) because there is no myIter.

    I researched the mem_fun family but I'm still confused.

    Broadly speaking, how can I get access to the member functions of each element being implicitly passed into the above function algo_if?

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    > What if I didn't want to pass in the element of a container...what if I wanted to pass in the string length of each element?!

    I don't think this should be a problem. For example, if you were using find_if on a container of strings, and you wanted to find an element with length equal to 7, you can fairly easily write your own functor, e.g.:
    Code:
    #include <string>
    #include <algorithm>
    #include <functional>
    #include <iostream>
    #include <vector>
    
    template <class T>
    struct length_is_equal: public std::binary_function<T, typename T::size_type, bool>
    {
      bool operator()(T obj, typename T::size_type len) const
      {
        return obj.length()==len;
      }
    };
    
    
    int main()
    {  
      std::vector<std::string> strings;
      strings.push_back("Hello");
      strings.push_back("World");
      strings.push_back("abcefg");
      std::vector<std::string>::iterator it = std::find_if(strings.begin(),strings.end(), std::bind2nd(length_is_equal<std::string>(),5));
      if (it!=strings.end())
        std::cout<<*it;
    }
    Granted, the syntax can be a bit verbose/ugly, but it's not too difficult when you get used to it. (And if you use boost, it might be able to simplify things somewhat)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 05:18 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM