0

我正在尝试为二维向量上的二进制搜索程序编写代码。我试过代码。但我不明白我有什么错误。我在下面给出了我的代码和错误。

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>&}’
4

1 回答 1

0

我认为您的运算符重载中的论点应该颠倒过来。

bool operator()(const vector<string>& rhs, const string& lhs)

由于编译器正在寻找 vector< string > 作为第一个参数

>  /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])’ 
于 2014-01-11T08:05:46.080 回答