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?

4

2 に答える 2

1

おそらく、あなたができる最善の方法は、 Date に operator< を定義し、アルゴリズムで述語を明示的に使用しないことです

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);
于 2014-04-01T08:32:36.683 に答える
1

比較オブジェクトのシグネチャはです。の引数bool cmp(const Type1 &a, const Type2 &b);に追加する必要があります。constmyCompare

于 2014-04-01T08:28:21.163 に答える