【发布时间】:2017-03-19 09:16:37
【问题描述】:
我在使用 STL 下限函数时遇到了一些问题。我是 C++ 新手。我需要对 Biz 类对象的向量进行排序,所以我使用了这种排序:
bool cmpID(const Biz & a, const Biz & b) {
return a.bizTaxID < b.bizTaxID;
}
sort(bussiness_list.begin(), bussiness_list.end(), cmpID);
问题是当我尝试在另一个具有lower_bound 的函数中通过bizTaxID 查找对象Biz 时。我以为我可以为此使用相同的函数cmpID,但显然不行:
taxID = itax; //function parameter, I am searching for the `Biz` with this ID
auto it = lower_bound(bussiness_list.begin(), bussiness_list.end(), taxID, cmpID);
我得到一个编译器错误:'bool (const Biz &,const Biz &)': cannot convert argument 2 from 'const std::string' to 'const Biz &'
我认为我可以使用相同的比较函数进行搜索和排序。有人可以向我解释错误在哪里,lower_bound 究竟需要我传递什么?正如我所说,我是 C++ 新手。
提前谢谢你。
【问题讨论】:
标签: c++ stl binary-search