Binary Search for 2D vector

1k Views Asked by At

I am trying to write code for Binary search program on a 2D vector. I tried code. But I can't understand what is the error I have got. I have given below my code and errors.

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";
}

I got the below errors.

>  /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>&}’
1

There are 1 best solutions below

2
On

I think the arguements in your operator overload should be reversed.

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

As the compiler is looking for the vector< string > as the first argument

>  /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])’