2009-05-17 3 views
2

이 코드를 구현했지만 컴파일시 오류가 발생했습니다 (VC2008 Express Ed.) :
이제 모든 코드가 있습니다.hash_map을 사용하여 실패 <vector<int>, string>, 이유가 무엇입니까?


#include <stdio.h> 
#include <vector> 
#include <string> 
#include <hash_map> 

using namespace std; 
using namespace stdext; 

typedef vector type_key; 

int main(int argc, char* argv[]) 
{ 
    type_key key; 

    hash_map<type_key, string> map_segment; 
    hash_map<type_key, string>::iterator iter_map_segment; 

    iter_map_segment = map_segment.find(key); 

    return 0; 
} 

unordered_map을 사용할 경우에도 같은 오류가 발생합니다.

하지만 맵 오류로 컨테이너를 교체하지 않습니다.
해결하기 위해 수행 할 수있는 작업은 무엇입니까?
감사합니다.

[EDITED]
오류 정보 :


------ Build started: Project: map_key_test, Configuration: Debug Win32 ------ 
Compiling... 
map_key_test.cpp 
c:\program files\microsoft visual studio 9.0\vc\include\xhash(75) : error C2440: 'type cast' : cannot convert from 'const type_key' to 'size_t' 
     No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 
     c:\program files\microsoft visual studio 9.0\vc\include\xhash(128) : see reference to function template instantiation 'size_t stdext::hash_value<_Kty>(const _Kty &)' being compiled 
     with 
     [ 
      _Kty=type_key 
     ] 
     c:\program files\microsoft visual studio 9.0\vc\include\xhash(127) : while compiling class template member function 'size_t stdext::hash_compare<_Kty,_Pr>::operator()(const _Kty &) const' 
     with 
     [ 
      _Kty=type_key, 
      _Pr=std::less<type_key> 
     ] 
     c:\program files\microsoft visual studio 9.0\vc\include\hash_map(78) : see reference to class template instantiation 'stdext::hash_compare<_Kty,_Pr>' being compiled 
     with 
     [ 
      _Kty=type_key, 
      _Pr=std::less<type_key> 
     ] 
     c:\program files\microsoft visual studio 9.0\vc\include\xhash(191) : see reference to class template instantiation 'stdext::_Hmap_traits<_Kty,_Ty,_Tr,_Alloc,_Mfl>' being compiled 
     with 
     [ 
      _Kty=type_key, 
      _Ty=std::string, 
      _Tr=stdext::hash_compare<type_key,std::less<type_key>>, 
      _Alloc=std::allocator<std::pair<const type_key,std::string>>, 
      _Mfl=false 
     ] 
     c:\program files\microsoft visual studio 9.0\vc\include\hash_map(88) : see reference to class template instantiation 'stdext::_Hash<_Traits>' being compiled 
     with 
     [ 
      _Traits=stdext::_Hmap_traits<type_key,std::string,stdext::hash_compare<type_key,std::less<type_key>>,std::allocator<std::pair<const type_key,std::string>>,false> 
     ] 
     c:\users\salamon\documents\visual studio 2008\projects\map_key_test\map_key_test.cpp(17) : see reference to class template instantiation 'stdext::hash_map<_Kty,_Ty>' being compiled 
     with 
     [ 
      _Kty=type_key, 
      _Ty=std::string 
     ] 
Build log was saved at "file://c:\Users\Salamon\Documents\Visual Studio 2008\Projects\map_key_test\Debug\BuildLog.htm" 
map_key_test - 1 error(s), 0 warning(s) 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

[EDITED]
완벽한 솔루션 :


#include <stdio.h> 
#include <vector> 
#include <string> 
#include <hash_map> 
using namespace std; 
using namespace stdext; 

class Vector : public vector<int> 
{ 
public: 
    operator size_t() const { return (*this).size(); }; 
}; 
typedef Vector type_key; 

struct less_vector 
{ 
    bool operator()(const Vector & x, const Vector & y) const 
    { 
     if (x != y) 
      return true; 

     return false; 
    } 
}; 

struct greater_vector 
{ 
    bool operator()(const Vector & x, const Vector & y) const 
    { 
     if (x == y) 
      return true; 

     return false; 
    } 
}; 

int main(int argc, char* argv[]) 
{ 
    Vector key; 
    string str; 

    hash_map<Vector, string, hash_compare <Vector, less_vector > > map_segment; 
    hash_map<Vector, string, hash_compare <Vector, greater_vector > >::iterator iter_map_segment; 

    // 
    key.push_back(0); 
    key.push_back(1); 
    key.push_back(2); 
    str = "012"; 
    map_segment [key] = str; 
    // 
    key.clear(); 
    key.push_back(1); 
    key.push_back(0); 
    key.push_back(2); 
    str = "102"; 
    map_segment [key] = str; 
    // 
    key.clear(); 
    key.push_back(2); 
    key.push_back(1); 
    key.push_back(0); 
    str = "210"; 
    map_segment [key] = str; 
    // 
    key.clear(); 
    key.push_back(1); 
    key.push_back(0); 
    key.push_back(2); 

    iter_map_segment = map_segment.find(key); 

    return 0; 
} 
+1

컴파일러에서 제공하는 오류 텍스트를 게시하십시오. 컴파일러 오류는 문제를 디버그하는 데 도움이되는 많은 정보를 제공합니다. 또한 향후에 동일한 오류가 발생하여이를 찾는 사용자에게 도움이 될 것입니다. –

답변

4

먼저 포함 헤더가 정말 존재하는지 확인 설치.

두 번째로 hash_map은 stdext 네임 스페이스에 있으므로이 이름을 명시 적으로 stdext::hash_map으로 지정하거나 using 지정 문을 통해 표시하도록 설정해야합니다.

셋째, 코드에서 hash_map의 템플릿 인수는 어디에 있습니까? 나는 hash_map만을보고 hash_map<vector<int>,string>을 볼 수 없습니다.

넷째, std :: vector를 해시 키의 해시 키로 사용할 수 있는지 확실하지 않습니다. 키의 해시 함수를 정의해야합니다 (hash_compare 함수 개체를 구현하고 세 번째 템플릿 매개 변수로 hash_map에 전달). 어쨌든 이러한 벡터를 값으로 저장하는 것은 좋은 생각이 아니며, 대신이 벡터에 대한 포인터를 키로 사용하는 것이 좋습니다.

1

물고기가 나는 경우에도 냄새가납니다 ... 해시 맵에서 문자열 속성을 검색하기 위해 벡터 -int-int를 키로 사용하려는 상황이 있습니까?

나에게 좋지 않은 디자인이나 좋지 않은 소리처럼 들립니다. 처음에는 모든 해시 코드를 계산하는 비용에 대해 생각해보십시오.

+0

나는 귀하의 체중을 평가할 것입니다. 감사합니다. – lsalamon

+0

int 벡터를 기반으로하는 문자열을 찾는 것이 비웃음 인 것 같습니다. 아마 다른 방향 일거야? – luke

+0

이상하게 보일 수도 있지만 그렇지 않습니다. – lsalamon

0

키를 비교하려면 <, ==,> 연산자를 정의해야합니다. 여기서 find() 함수는 키를 비교하는 방법을 알지 못합니다.

관련 문제