2017-05-10 6 views
-3

저는 파이썬/자바 토지에서 왔습니다. 이제는 C++에서 내 자신의 hashmap을 만드는 방법을 고민 중입니다.벡터 객체를 NULL과 비교할 수 있습니까?

invalid operands to binary expression ('value_type' (aka 'HashEntry') and 'long')" 

나는 누군가가 나를 조종 할 수 있습니다 바라고 있어요 : 나는 (라인 6, 11) NULL로 해시 테이블에서 위치의 위치를 ​​비교 확인하려고하면

, 나는 오류 내가 뭘 잘못하고 어떻게 해결할 수 있는지에 대한 방향.

void CustomHash::insert(HashEntry entry) { 
    int k = entry.getKey(); 
    int i = 0; 
    int hashVal = hash_One(k); 
    int temp = hashVal; 
    while (i < size && hashArray[temp] != NULL) { 
     i++; 
     temp = (hashVal + i*hash_Two(k)) % size; 
    } 

    if (hashArray[temp] == NULL) { 
     hashArray[temp] = entry; 
    } 
    else { 
     cout << "Failure" << endl; 
    } 
} 

편집 1 : hashArray 선언을 감안할 때

CustomHash::CustomHash(int m) { 
    this->size = m; 
    this->hashArray = vector<HashEntry>(); 
} 
+3

컴파일러에서 오류라고합니다. 'hasArray'의 선언을 보여 주면 누군가가 해결책을 제시 할 수 있을지도 모릅니다. –

+0

'hashArray.count (temp)' – Justin

+0

@Justin André가 연습으로 자신의 해시 맵을 작성하고 있습니다. 'hashArray'는 표준 해시 맵이 아닌 해당 유형이라고 가정하는 것이 합리적입니다. 따라서,'count' 메쏘드가 있다고 가정 할 수 없습니다. –

답변

0

,

hashArray is a vector<HashEntry> Object

hashArray[temp]HashEntry로 평가합니다. HashEntryNULL을 비교할 수 없습니다.

키가 temp 인 항목이 hasArray에 있는지 확인하려면 다른 전략을 사용하십시오. std::count_if을 사용하는 것이 좋습니다.

while (i < size && std::count_if(hashArray.begin(), hashArray.end(), 
           [=temp](HashEntry const& e) 
           { return (temp == e.getKey());}) > 0) 
{ 
    i++; 
    temp = (hashVal + i*hash_Two(k)) % size; 
} 

@cdhowie가 제안한 더 나은 해결책은 std::any_of입니다.

while (i < size && std::any_of(hashArray.begin(), hashArray.end(), 
           [=temp](HashEntry const& e) 
           { return (temp == e.getKey());})) 
{ 
    i++; 
    temp = (hashVal + i*hash_Two(k)) % size; 
} 
+0

조건이'> 0'이므로'std :: any_of'는'std :: any_of'를 제안합니다 - 일치 항목을 찾으면 조기에 멈출 수 있습니다. – cdhowie

+0

@cdhowie, 감사합니다. 더 나은 솔루션입니다. –