![](/img/trans.png)
[英]using lower_bound or upper_bound on Structure in C++
[英]upper_bound/lower_bound function in C++
我试图使用这些函数找到我的矢量的上限和下限( 矢量可能 )。 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);
但通过这样做,编译器显示以下消息:
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);
什么是使用这些功能的正确方法?
比较对象的签名是bool cmp(const Type1 &a, const Type2 &b);
,你需要将const
添加到myCompare
的参数中。
也许您可以做的最好的事情是定义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.