2014-01-11 9 views
0

2D 벡터에 이진 검색 프로그램 용 코드를 쓰려고합니다. 나는 코드를 시도했다. 그러나 나는 내가 가진 오류가 무엇인지 이해할 수 없다. 나는 나의 코드와 오류 아래에 주었다.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

나는 연산자 오버로드의 주장이 반대라고 생각합니다.

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

컴파일러는 함수 정의를 변경 한 후 첫 번째 인수

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

로, 나는 아래의 오류가 발생했습니다> 벡터 < 문자열을 찾고있다. /usr/include/c++/4.6/bits/stl_algo.h : 함수 'bool std :: binary_search (_File, _Filter, const _Tp & _Compare) [_FIter = __gnu_cxx :: __ normal_iterator > *, std :: vector >>, _Tp = char [9], _Compare = BINARY_ON] ': /usr/include/c++/4.6/bits/ stl_algo.h : 2714 : 56 : 오류 : '(BINARY_ON) (BINARY_ON) (const char [9], std :: vector > &')의 호출에 일치하지 않습니다. –

+0

C++ 하드 코딩 된 문자열은 char . 그러므로 const char [9]. binary_search 함수에 전달하기 전에 "01051996"을 유형 문자열로 변환하십시오. 당신은 또한 const char []를 사용하도록 함수 정의를 변경할 수 있지만, 그것은 함수에 대한 코드 변경을 요구할 수 있습니다. –