0

I'm trying to find the upper and lower bounds of my vector (vector possible) using these functions. The struct data holds 3 strings and I'm using string date for comparison.

bool myCompare(Data &a, Data &b) {
      return (a.date == b.date);
}

#include <algorithm>

     std::vector<Data>::iterator iterl, iteru;
     sort(possible.begin(), possible.end(), compare);
     iterl = std::lower_bound(possible.begin(), possible.end(), struct1, myCompare);
     iteru = std::upper_bound(possible.begin(), possible.end(), struct2, myCompare);

but by doing that, the compiler is displayng the following message:

Main.cpp:95:18: note: in instantiation of function template specialization 'std::__1::upper_bound<std::__1::__wrap_iter<data *>,
data, bool (*)(data &, data &)>' requested here
iteru = std::upper_bound(possible.begin(), possible.end(), struct2, myCompare);

whats the proper way to use these functions?

brokenfoot
  • 11,083
  • 10
  • 59
  • 80

2 Answers2

1

The signature for the comparison object is bool cmp(const Type1 &a, const Type2 &b);, you need to add const to the arguments of myCompare.

user657267
  • 20,568
  • 5
  • 58
  • 77
1

Perhaps the best that you can do is to define operator< for Date and don't use predicates with algorithm explicitely

bool operator<(const Data& lhs, const Data& rhs)
{
    return lhs.date < rhs.date;
}

std::vector<Data>::iterator iterl, iteru;
sort(possible.begin(), possible.end());
iterl = std::lower_bound(possible.begin(), possible.end(), data1);
iteru = std::upper_bound(possible.begin(), possible.end(), data2);
AlexT
  • 1,413
  • 11
  • 11