我正在尝试在2D矢量上编写二进制搜索程序的代码。我试过代码。但我无法理解我得到的错误是什么。我在下面给出了我的代码和错误。
class BINARY_ON
{
public:
explicit BINARY_ON(int column,string fCol) :m_column(column),fColType(fCol) {}
bool operator()(const string& lhs,const vector<string>& rhs)
{
if(fColType=="number")
return atoi(lhs.c_str()) < atoi(rhs[m_column].c_str());
else if(fColType=="date")
return PPCheckTwoDate(lhs, rhs[m_column])<0;
else
return lhs < rhs[m_column];
}
private:
int m_column;
string fColType;
};
int main ()
{
vector<vector<string>> table_data;
// Vector Data Insert
BINARY_ON compare_column(4,"date");
if (std::binary_search (table_data.begin(), table_data.end(), "01051996", compare_column))
std::cout << "found!\n"; else std::cout << "not found.\n";
}
我收到了以下错误。
> /usr/include/c++/4.6/bits/stl_algo.h:2416:4: error: no match for call
> to ‘(PPBINARY_ON) (std::vector<std::basic_string<char> >&, const char
> [9])’
>
> note: bool PPBINARY_ON::operator()(const string&, const
> std::vector<std::basic_string<char> >&)
>
> note: no known conversion for
> argument 1 from ‘std::vector<std::basic_string<char> >’ to ‘const
> string& {aka const std::basic_string<char>&}’
答案 0 :(得分:0)
我认为运算符重载中的参数应该颠倒过来。
bool operator()(const vector<string>& rhs, const string& lhs)
当编译器正在寻找向量&lt;串GT;作为第一个论点
> /usr/include/c++/4.6/bits/stl_algo.h:2416:4: error: no match for call
> to ‘(PPBINARY_ON) (std::vector<std::basic_string<char> >&, const char
> [9])’