CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: predicate function

  1. #1
    Join Date
    May 2008
    Posts
    36

    Unhappy predicate function

    I bet there should be times you need to use a predicate not only with 2 parameters but more than that. I am now wondering how can I pass in an algorithm's function a predicate with 3 or more parameters.

    That's the point where my knowledge about STL stops, could someone clear it up with new roads from here ?

    Thank you.

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    Re: predicate function

    Why don't you try posting an example of the situation you are talking about.

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: predicate function

    I'm not really certain about your scenario either, but it looks like what you want is a functor instead of a function as the predicate. Its operator() would then take the usual parameters passed to the predicate by the STL algorithm, and any other information your functor object needs can be set up any way you want before invoking the algorithm.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: predicate function

    I don't think any of the existing STL algorithms require a predicate with more than 2 arguments.

  5. #5
    Join Date
    May 2008
    Posts
    36

    Re: predicate function

    Thanks I always meet problems with more than 2

    For example,

    bool pred(T a, T b, bool c)
    {
    return c?a.x<b.y:a.x>b.y;
    }

    this is not allowed is , e.g remove_if

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: predicate function

    Code:
    struct CompareDim
    {
        bool cmpX;
        CompareDim(bool compareX)
            :cmpX(compareX)
        {}
        bool operator()(T a, T b) const
        {
               return cmpX ? a.x<b.y : a.x>b.y;
        }
    };
    
    int main()
    {
        vector<T> myvec;
        .....
        myvec.erase(
            remove_if(myvec.begin(),myvec.end(),CompareDim(true)),
            myvec.end());
    }
    Or, in C++0x lambda functions....
    Code:
    int main()
    {
        vector<T> myvec;
        .....
        bool cmpX = true;
        myvec.erase(
            remove_if(myvec.begin(),myvec.end(),[cmpX](T a, T b){ return cmpX ? a.x<b.y : a.x>b.y; }),
            myvec.end());
    }
    Last edited by Lindley; May 26th, 2011 at 05:17 PM.

  7. #7
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: predicate function

    Lindley, under which circumstances does remove_if() take a binary predicate?

    I had my proposed code sample just ready when the notification mail about your post came in, and now that I have it I want to post it... It is a functor-style unary predicate that takes the "other" T and the bool as constructor parameters:

    Code:
    class predicate
    {
    public:
      predicate(const T &b, bool c) : m_b(b), m_c(c)
      {}
    
      bool operator()(const T &a) const
      {
        return m_c ? a.x < m_b.y : a.x > m_b.y;
      }
    
    private:
      const T &m_b;
      bool m_c;
    };
    BTW, how does your first example work at all, as I don't see an operator() in there anywhere? Is there a third way to define predicates I don't know of yet?

    Actually, I, personally, find the lambda function example even easier to understand. I'm gradually approaching comprehension of them...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: predicate function

    Quote Originally Posted by Eri523 View Post
    Lindley, under which circumstances does remove_if() take a binary predicate?
    ...
    BTW, how does your first example work at all, as I don't see an operator() in there anywhere? Is there a third way to define predicates I don't know of yet?
    You're right, I goofed on the operator. And remove_if is not a good example of this predicate (perhaps std::sort would be better).

  9. #9
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: predicate function

    Quote Originally Posted by Lindley View Post
    You're right, I goofed on the operator.
    Ah, ok.

    And remove_if is not a good example of this predicate (perhaps std::sort would be better).
    Indeed, for std::sort() a binary predicate makes perfect sense. I'm not familiar enough with the STL algorithms to reliably tell off the top of my head which of them require a binary predicate and which a unary one (given it requires a predicate at all).

    I'm curious about what would happen when remove_if() actually would get presented a binary predicate. I somehow suppose it would throw some sort of "operator not defined" error during template instantiation, right?

    Of course it would be no problem to define a functor which is both a unary and a binary predicate at the same time. This would provide allround compatibility though I have only a vague idea of what that might be useful for. (However, I don't have the slightest idea of which practical use the predicate skeleton presented by the OP in post #5 might be either.)

    While it's clearly not possible to simply take the address of an overloaded function due to overload resolution ambiguity, it might be even possible to use overloaded unary and binary function-style predicates of the same name for the STL algorithms due to the way template instantiation works (at compile-time!). However, this is just wild speculation and I can't imagine any practical use for that either.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: predicate function

    Quote Originally Posted by Eri523
    it might be even possible to use overloaded unary and binary function-style predicates of the same name for the STL algorithms due to the way template instantiation works (at compile-time!). However, this is just wild speculation and I can't imagine any practical use for that either.
    Maybe such a dual purpose predicate can be used with std::transform

    Incidentally, by "function-style predicates" I think you mean "function object predicates".
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: predicate function

    Quote Originally Posted by laserlight View Post
    Maybe such a dual purpose predicate can be used with std::transform
    Ah! I was looking for an example of this and std::transform() definitely is one. Thanks.

    Incidentally, by "function-style predicates" I think you mean "function object predicates".
    Err... Unless I misunderstand you here: no. To those you mean I was referring as "functor-style". Maybe I should brush up my terminology regarding this area of C++ a bit?
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  12. #12
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: predicate function

    Quote Originally Posted by Eri523
    Err... Unless I misunderstand you here: no. To those you mean I was referring as "functor-style". Maybe I should brush up my terminology regarding this area of C++ a bit?
    Yes, because in C++ terminology, function object and functor are synonymous
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  13. #13
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: predicate function

    Quote Originally Posted by laserlight View Post
    Yes, because in C++ terminology, function object and functor are synonymous
    Well, that's why I used the term "fuction-style" as opposed to "functor-style". I never wrote "function-object-style" as a third variant which of course woud've pretty much messed up the whole thing.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)