我有一个小的工作代码,用于查找使用特殊比较方法的一系列项目。但是,当我尝试使用lower_bound()
和upper_bound()
函数重写它时,出现一个奇怪的错误。我写了一个小代码来说明我的问题。这是代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
int main() {
string str = "banana";
string keyword = "ana";
int sa_idx[] = {5, 3, 1, 0, 4, 2};
vector<int> sa(sa_idx, sa_idx + sizeof(sa_idx) / sizeof(int) );
auto cmp = [&str] (const int &a, const string &keyword) -> bool
{
return strncmp(str.c_str() + a, keyword.c_str(), keyword.length()) < 0;
};
cout << (upper_bound(sa.begin(), sa.end(), keyword, cmp) -
lower_bound(sa.begin(), sa.end(), keyword, cmp)) << endl;
return 0;
}
如您所见,比较函数使用sa
数组的关键字和值进行比较决策。 standard说:
Type1类型必须使得ForwardIt类型的对象可以是 解引用,然后隐式转换为Type1。类型Type2 必须使得类型T的对象可以隐式转换为 Type2。
我的比较函数的第一个参数的类型为int
(因为数组的vector<int>
),第二个参数类型的类型为string
(作为关键字的类型)。但我不知道为什么会出现以下错误:
In file included from /usr/include/c++/6/bits/stl_algobase.h:71:0,
from /usr/include/c++/6/bits/char_traits.h:39,
from /usr/include/c++/6/ios:40,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from prog.cpp:1:
/usr/include/c++/6/bits/predefined_ops.h: In instantiation of ‘bool __gnu_cxx::__ops::_Val_comp_iter<_Compare>::operator()(_Value&, _Iterator) [with _Value = const std::__cxx11::basic_string<char>; _Iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare = main()::<lambda(const int&, const string&)>]’:
/usr/include/c++/6/bits/stl_algo.h:2049:14: required from ‘_ForwardIterator std::__upper_bound(_ForwardIterator, _ForwardIterator, const _Tp&, _Compare) [with _ForwardIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Tp = std::__cxx11::basic_string<char>; _Compare = __gnu_cxx::__ops::_Val_comp_iter<main()::<lambda(const int&, const string&)> >]’
/usr/include/c++/6/bits/stl_algo.h:2114:32: required from ‘_FIter std::upper_bound(_FIter, _FIter, const _Tp&, _Compare) [with _FIter = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Tp = std::__cxx11::basic_string<char>; _Compare = main()::<lambda(const int&, const string&)>]’
prog.cpp:19:57: required from here
/usr/include/c++/6/bits/predefined_ops.h:173:11: error: no match for call to ‘(main()::<lambda(const int&, const string&)>) (const std::__cxx11::basic_string<char>&, int&)’
{ return bool(_M_comp(__val, *__it)); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.cpp:14:61: note: candidate: main()::<lambda(const int&, const string&)>
auto cmp = [&str] (const int &a, const string &keyword) -> bool
^~~~
prog.cpp:14:61: note: no known conversion for argument 1 from ‘const std::__cxx11::basic_string<char>’ to ‘const int&’
我错过了非常明显的事情吗?因为似乎编译器会寻找string
作为比较函数的第一个参数。
答案 0 :(得分:5)
您的问题是func uppdateFavorites() {
self.favoritosVC.readUserFromCD({_ in
self.favoritosVC.tableView.reloadData()
})
}
要求std::uppper_bound
具有签名
cmp
bool(T, decltype(*Iterator))
却有相反的要求,并且想要
std::lower_bound
,因此您不能在两个函数中使用相同的比较器。还有其他修改代码的方法,只是添加第二个比较器,如
bool(decltype(*Iterator), T)
允许代码进行编译。