[英]upper_bound/lower_bound function in C++
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. struct数据包含3个字符串,我使用字符串日期进行比较。
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? 什么是使用这些功能的正确方法?
The signature for the comparison object is bool cmp(const Type1 &a, const Type2 &b);
比较对象的签名是bool cmp(const Type1 &a, const Type2 &b);
, you need to add const
to the arguments of myCompare
. ,你需要将const
添加到myCompare
的参数中。
Perhaps the best that you can do is to define operator< for Date and don't use predicates with algorithm explicitely 也许您可以做的最好的事情是定义operator <for Date并且不要明确地使用带有算法的谓词
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.