Re: Predicate with argument

Discussion in 'C++' started by Ian Collins, Feb 5, 2009.

  1. Ian Collins

    Ian Collins Guest

    Christopher wrote:
    > How do I use std::find_if with a predicate that takes an argument?
    >

    Include the value as a member of the predicate and initialise it in the
    constructor.

    For example:

    struct CompareTag
    {
    const std::string& tag;

    CompareTag( const std::string& tag ) : tag(tag) {}

    bool operator()( const Node& node ) const
    {
    return node.name() == tag;
    }
    };

    --
    Ian Collins
     
    Ian Collins, Feb 5, 2009
    #1
    1. Advertising

  2. Ian Collins

    Christopher Guest

    On Feb 5, 3:52 pm, Ian Collins <> wrote:
    > Christopher wrote:
    > > How do I use std::find_if with a predicate that takes an argument?

    >
    > Include the value as a member of the predicate and initialise it in the
    > constructor.
    >
    > For example:
    >
    >    struct CompareTag
    >    {
    >      const std::string& tag;
    >
    >      CompareTag( const std::string& tag ) : tag(tag) {}
    >
    >      bool operator()( const Node& node ) const
    >      {
    >        return node.name() == tag;
    >      }
    >    };
    >
    > --
    > Ian Collins



    I tried that and it doesn't it won't compile.
    I must not be using it correctly.

    Error "term does not evaluate to a function taking 0 arguments"
    On the std::find_if line of code.

    How do I use the predicate in this case?



    Here is my code:

    // Make a copy of the provided buffer pointers and remove them as
    they are matched with the requirements and stored
    std::vector<Buffer::SharedPtr> providedBuffers = vertexBuffers;

    // Go though the requirements and look for a match from the
    provided buffers
    unsigned inputSlot = 0;

    while( !requirements.empty() )
    {
    // Get the first requirement
    std::string semanticName = requirements.front().SemanticName;

    // Search for a buffer with a matching semantic name in the
    provided buffers
    CompareSemanticName comparator(semanticName);

    std::vector<Buffer::SharedPtr> found =
    std::find_if(providedBuffers.begin(), providedBuffers.end(),
    comparator());

    if( found == providedBuffers.end() )
    {
    // Cannot provide the required vertex buffer type
    std::stringstream msg;
    msg << "The provided vertexs buffers do not contain a vertex
    buffer type required "
    << "by the chosen technique."
    << "\nSemanticName: " << semanticName;

    throw BaseException(msg.str(),
    "PassDescription::passDescription(Pass & pass, const
    std::vector<Buffer::SharedPtr> & vertexBuffers, const
    Buffer::SharedPtr & indexBuffer, const D3D10_PRIMITIVE_TOPOLOGY
    primitiveTopology)",
    "PolygonSet3D.cpp");
    }


    // Loop under construction
    }
     
    Christopher, Feb 5, 2009
    #2
    1. Advertising

  3. Ian Collins

    Ian Collins Guest

    Christopher wrote:
    > On Feb 5, 3:52 pm, Ian Collins <> wrote:
    >> Christopher wrote:
    >>> How do I use std::find_if with a predicate that takes an argument?

    >> Include the value as a member of the predicate and initialise it in the
    >> constructor.
    >>
    >> For example:
    >>
    >> struct CompareTag
    >> {
    >> const std::string& tag;
    >>
    >> CompareTag( const std::string& tag ) : tag(tag) {}
    >>
    >> bool operator()( const Node& node ) const
    >> {
    >> return node.name() == tag;
    >> }
    >> };
    >>

    Plesae don't quote signatures.
    >
    > I tried that and it doesn't it won't compile.
    > I must not be using it correctly.
    >
    > Error "term does not evaluate to a function taking 0 arguments"
    > On the std::find_if line of code.


    > CompareSemanticName comparator(semanticName);


    Here you create a CompareSemanticName object.

    > std::vector<Buffer::SharedPtr> found =
    > std::find_if(providedBuffers.begin(), providedBuffers.end(),
    > comparator());


    Here you do something odd! What's "comparator()"?

    A complete simple example:

    #include <string>
    #include <vector>
    #include <iostream>

    struct CompareTag
    {
    const std::string tag;

    CompareTag( const std::string& tag ) : tag(tag) {}

    bool operator()( const std::string& node ) const
    {
    return node == tag;
    }
    };

    int main()
    {
    std::vector<std::string> vec;

    vec.push_back("hello");
    vec.push_back("there");
    vec.push_back("world");

    std::cout << *std::find_if( vec.begin(), vec.end(),
    CompareTag("there") )
    << std::endl;
    }


    --
    Ian Collins
     
    Ian Collins, Feb 5, 2009
    #3
  4. Ian Collins

    Christopher Guest

    > A complete simple example:
    >
    > #include <string>
    > #include <vector>
    > #include <iostream>
    >
    > struct CompareTag
    > {
    >    const std::string tag;
    >
    >    CompareTag( const std::string& tag ) : tag(tag) {}
    >
    >    bool operator()( const std::string& node ) const
    >    {
    >      return node == tag;
    >    }
    >
    > };
    >
    > int main()
    > {
    >    std::vector<std::string> vec;
    >
    >    vec.push_back("hello");
    >    vec.push_back("there");
    >    vec.push_back("world");
    >
    >    std::cout << *std::find_if( vec.begin(), vec.end(),
    >                                CompareTag("there") )
    >              << std::endl;
    >
    > }


    Thanks Ian! Got it to work!
     
    Christopher, Feb 5, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jacques

    Predicate by attribute?

    Jacques, Jun 24, 2004, in forum: XML
    Replies:
    2
    Views:
    378
    Jacques
    Jun 24, 2004
  2. awebguynow
    Replies:
    1
    Views:
    359
    George Bina
    Jan 27, 2006
  3. Sabba
    Replies:
    0
    Views:
    631
    Sabba
    May 9, 2006
  4. mikets
    Replies:
    18
    Views:
    1,156
    Chris \( Val \)
    Jan 7, 2004
  5. Christopher

    Predicate with argument

    Christopher, Feb 5, 2009, in forum: C++
    Replies:
    0
    Views:
    263
    Christopher
    Feb 5, 2009
Loading...

Share This Page