2

I'm trying to find an element in a std::vector based in a member function, but unforunately I have no access to a full C++11 conformant compiler.

I kwnow I can use a functor to solve this, but I wonder if there is a "functional" way to accomplish the same result.

Below is a snippet which depicts my problem:

#include <iostream>
#include <string>
#include <functional>
#include <algorithm>
#include <vector>

struct Class {
  int type_;

  Class(int type): type_(type) {
  }

  int GetType() {
    return type_;
  }
};

struct Functor {
  Functor(int t): t_(t) {
  }

  bool operator()(Class c) {
    return c.GetType() == t_;
  }

  int t_;
};

int main() {    
  // It also works
  std::vector<Class> v2 { Class(1), Class(2), Class(3), Class(4), Class(5) };
  auto it2 = std::find_if(v2.begin(), v2.end(), Functor(4));
  std::cout << (it2 != v2.end() ? "Found!" : "Not found!") << std::endl;

  // It would solve, but I can't use due to compiler limitations :(
  it2 = std::find_if(v2.begin(), v2.end(), [](auto& v) { return v.GetType() == 4; });
  std::cout << (it2 != v2.end() ? "Found!" : "Not found!") << std::endl;

  // Is there any "functional based" solution to this, using std::mem_fun, std::bind1st, etc.?
  // it2 = std::find_if(v2.begin(), v2.end(), ???);

  return 0;
}

if my std::vector was formed by a non-complex type, I would do something like:

  std::vector<int> v1 { 1, 2, 3, 4, 5 };
  auto it1 = std::find_if(v1.begin(), v1.end(), std::bind1st(std::equal_to<int>(), 4));
  std::cout << (it1 != v1.end() ? "Found!" : "Not found!") << std::endl;

Is there any solution do write a code similar to that above?

Edit:

I'm using GCC 4.4.1

Edit2:

Based in some comments and in the response from @scohe001, I would solve the problem overloading the global == operator.

But my curiosity isn't satisfied yet :) Is there no way to achieve my goal using the std toolset from <funtional>?

Edit3:

Only to clarify: After reading the responses and comments, I know that it's possible to solve the simple example I posted before using the overloading of the operator==(int) and also know that I can use a function object (functor) to do the same job of the lambda expression. But, my real question is: Using ONLY the toolset available in <functional> (std::mem_fun, std::bind1st, std::equal_to, etc), can I "mimic" the behavior of the lambda/functor? If so, how can I "chain" the funtion calls to do it?

Edit4:

Apparently there's no way to solve my problem ONLY using the existing toolset from <functional>, so I'm accepting the @Caleth's response, once it's the one which is closer to what I was trying to do.

Valmir
  • 401
  • 1
  • 5
  • 14

2 Answers2

1

You'd have to write a bind_both adaptor yourself

it2 = std::find_if(v2.begin(), v2.end(), bind_both(std::equal_to<int>(), std::mem_fn_ref(&Class::getType), 4));

And it would have a combinatorial explosion of possibilities

template <typename Binary, typename Left, typename Arg>
class bind_left_t : public std::unary_function<Arg, typename Binary::result_type> {
    Binary b;
    Left l;
    typename Binary::second_argument_type r;
public:
    bind_left_t(Binary b, Left l, typename Binary::second_argument_type r) : b(b), l(l), r(r) {}
    typename Binary::result_type operator()(      Arg & arg) const { return b(l(arg), r); }
    typename Binary::result_type operator()(const Arg & arg) const { return b(l(arg), r); }
};

template <typename Binary, typename Right, typename Arg>
class bind_right_t : public std::unary_function<Arg, typename Binary::result_type> {
    Binary b;
    typename Binary::first_argument_type l;
    Right r;
public:
    bind_right_t(Binary b, typename Binary::first_argument_type l, Right r) : b(b), l(l), r(r) {}
    typename Binary::result_type operator()(      Arg & arg) const { return b(l, r(arg)); }
    typename Binary::result_type operator()(const Arg & arg) const { return b(l, r(arg)); }
};

template <typename Binary, typename Left, typename Right, typename Arg1, typename Arg2>
class bind_both_t : public std::binary_function<Arg1, Arg2, typename Binary::result_type> {
    Binary b;
    Left l;
    Right r;
public:
    bind_both_t (Binary b, Left l, Right r) : b(b), l(l), r(r) {}
    typename Binary::result_type operator()(      Arg1 & arg1,       Arg2 & arg2) const { return b(l(arg1), r(arg2)); }
    typename Binary::result_type operator()(const Arg1 & arg1,       Arg2 & arg2) const { return b(l(arg1), r(arg2)); }
    typename Binary::result_type operator()(      Arg1 & arg1, const Arg2 & arg2) const { return b(l(arg1), r(arg2)); }
    typename Binary::result_type operator()(const Arg1 & arg1, const Arg2 & arg2) const { return b(l(arg1), r(arg2)); }
};

The extra template arguments (Arg, Arg1 and Arg2) disambiguate between the three forms when calling bind_both

template <typename Binary, typename Left>
bind_left_t<Binary, Left, typename Left::argument_type> bind_both(Binary b, Left l, typename Binary::second_argument_type r)
{
    return bind_left_t<Binary, Left, typename Left::argument_type>(b, l, r);
}

template <typename Binary, typename Right>
bind_right_t<Binary, Right, typename Right::argument_type> bind_both(Binary b, typename Binary::first_argument_type l, Right r)
{
    return bind_right_t<Binary, Right, typename Right::argument_type>(b, l, r);
}

template <typename Binary, typename Left, typename Right>
bind_both_t<Binary, Left, Right, typename Left::argument_type, typename Right::argument_type> bind_both(Binary b, Left l, Right r)
{
    return bind_both_t<Binary, Left, Right, typename Left::argument_type, typename Right::argument_type>(b, l, r);
}
Caleth
  • 52,200
  • 2
  • 44
  • 75
0

It sounds like you can't modify the struct definition itself, so create an overload for operator== in the global scope:

bool operator==(const Class &lhs, const Class &rhs) {
    return lhs.type_ == rhs.type_;
}

Then you can use regular old find:

std::find(v2.begin(), v2.end(), Class(4));
scohe001
  • 15,110
  • 2
  • 31
  • 51