2 Replies - 1675 Views - Last Post: 08 May 2011 - 11:09 AM Rate Topic: -----

#1 munitjsr2   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 35
  • Joined: 02-April 11

function objects and predicate?

Posted 08 May 2011 - 10:26 AM

#include <iostream>
#include <vector>
#include <algorithm>

struct Foo {
    int val_;
    Foo(int v) : val_(v) {}
};

struct Pred {
    int want_;
    bool operator() (const Foo& f) { return want_ == f.val_; }
    Pred (int x) : want_(x) {}
};

int main () {
    std::vector< Foo > v;
    for (int i = 0; i < 5; ++i)
        v.push_back (Foo(i));
    std::cout << "length of vector: " << v.size () << std::endl;
    std::vector< Foo >::iterator vit;
    vit = std::find_if (v.begin (), v.end (), Pred(3));
    if (vit != v.end ()) {
        std::cout << "Found item to delete..." << std::endl;
        v.erase (vit);
    }
    std::cout << "length of vector: " << v.size () << std::endl;
    return 0;
}



trying to understand this
I went through google learnt a bit about function objects,predicate
function object :
A function object is an object to which the function call operator can be applied. Typically, it is a class that defines the function call operator (operator()()) as a member function. When a function object is used as a function, the function call operator is invoked whenever the function is called.

predicate :
A predicate is simply a function object that returns a value convertible to a bool

I am not able to comprehend this line :
find_if (v.begin (), v.end (), Pred(3)); //Line 22

v.begin() --> point's to the 1st vector element
v.end() --> point's to the next to last vector element
I can understand that Pred(3) is a constructor initializing want_ to 3 and its also invoking () operator then

the comparison want_ == f.val is happening but how? here's want_( is equal to 3) == f.val( how we are iterating through this).
its seems that Pred(3) call's many times.

please provide some expalanation or any example or a good source to learn such topic.

This post has been edited by munitjsr2: 08 May 2011 - 10:32 AM


Is This A Good Question/Topic? 0
  • +

Replies To: function objects and predicate?

#2 Jeff H   User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 308
  • Joined: 30-January 11

Re: function objects and predicate?

Posted 08 May 2011 - 10:37 AM

http://www.cplusplus...orithm/find_if/
http://www.cplusplus...l/vector/begin/
http://www.cplusplus...stl/vector/end/

This post has been edited by Jeff H: 08 May 2011 - 10:39 AM

Was This Post Helpful? 1
  • +
  • -

#3 munitjsr2   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 35
  • Joined: 02-April 11

Re: function objects and predicate?

Posted 08 May 2011 - 11:09 AM

View PostJeff H, on 08 May 2011 - 10:37 AM, said:



Thanks Jeff.H,

I got it
template<class InputIterator, class Predicate>
  InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred )
  {
    for ( ; first!=last ; first++ ) if ( pred(*first) ) break; //mine Pred(*number) where number=3
    return first;
  }



I am new to this and I had a hard time understanding this anyway.

This post has been edited by munitjsr2: 08 May 2011 - 11:13 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1