2017-01-12 82 views
1

所以我想在矢量中使用我的unordered_map中的鍵。我按照this answer,我有以下使用矢量作爲unordered_map中的鍵

#include <iostream> 
#include <cstdlib> 
#include <tuple> 
#include <unordered_map> 
#include <vector> 
#include <boost/functional/hash.hpp> /* hash_combine */ 
template <typename T> 

struct vectorHasher{ 
std::size_t operator()(std::vector<T> &in) const 
{ 
    using boost::hash_value; 
    using boost::hash_combine; 
    // Start with a hash value of 0 
    std::size_t seed = 0; 
    T value; 
    for (int i=0; i< in.size(); i++) 
    { 
     value = static_cast<T>(in[i]); 
     hash_combine(seed, hash_value(value)); 
    } 
    return seed; 
} 
}; 


int main() 
{ 
typedef std::unordered_map< std::vector<std::size_t>, int, vectorHasher<std::vector<std::size_t> > > map_type; 
map_type mymap; 

std::vector<size_t> vec (3,100); 
mymap[vec] = 1; 

return 0; 
} 

,但我得到了下面的錯誤編譯代碼。

In file included from mytest_vectorhasher.cpp:6: 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/unordered_map:404:17: error: 
    no matching function for call to object of type 'const 
    vectorHasher<std::__1::vector<unsigned long, std::__1::allocator<unsigned long> > 
    >' 
    {return static_cast<const _Hash&>(*this)(__x);} 
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__hash_table:1976:21: note: 
    in instantiation of member function 
    'std::__1::__unordered_map_hasher<std::__1::vector<unsigned long, 
    std::__1::allocator<unsigned long> >, 
    std::__1::__hash_value_type<std::__1::vector<unsigned long, 
    std::__1::allocator<unsigned long> >, int>, vectorHasher<std::__1::vector<unsigned 
    long, std::__1::allocator<unsigned long> > >, true>::operator()' requested here 
size_t __hash = hash_function()(__k); 
       ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/unordered_map:1443:21: note: 
    in instantiation of function template specialization 
    'std::__1::__hash_table<std::__1::__hash_value_type<std::__1::vector<unsigned long, 
    std::__1::allocator<unsigned long> >, int>, 
    std::__1::__unordered_map_hasher<std::__1::vector<unsigned long, 
    std::__1::allocator<unsigned long> >, 
    std::__1::__hash_value_type<std::__1::vector<unsigned long, 
    std::__1::allocator<unsigned long> >, int>, vectorHasher<std::__1::vector<unsigned 
    long, std::__1::allocator<unsigned long> > >, true>, 
    std::__1::__unordered_map_equal<std::__1::vector<unsigned long, 
    std::__1::allocator<unsigned long> >, 
    std::__1::__hash_value_type<std::__1::vector<unsigned long, 
    std::__1::allocator<unsigned long> >, int>, 
    std::__1::equal_to<std::__1::vector<unsigned long, std::__1::allocator<unsigned 
    long> > >, true>, 
    std::__1::allocator<std::__1::__hash_value_type<std::__1::vector<unsigned long, 
    std::__1::allocator<unsigned long> >, int> > 
    >::__emplace_unique_key_args<std::__1::vector<unsigned long, 
    std::__1::allocator<unsigned long> >, const std::__1::piecewise_construct_t &, 
    std::__1::tuple<const std::__1::vector<unsigned long, std::__1::allocator<unsigned 
    long> > &>, std::__1::tuple<> >' requested here 
return __table_.__emplace_unique_key_args(__k, 
       ^
mytest_vectorhasher.cpp:41:10: note: in instantiation of member function 
    'std::__1::unordered_map<std::__1::vector<unsigned long, 
    std::__1::allocator<unsigned long> >, int, vectorHasher<std::__1::vector<unsigned 
    long, std::__1::allocator<unsigned long> > >, 
    std::__1::equal_to<std::__1::vector<unsigned long, std::__1::allocator<unsigned 
    long> > >, std::__1::allocator<std::__1::pair<const std::__1::vector<unsigned long, 
    std::__1::allocator<unsigned long> >, int> > >::operator[]' requested here 
mymap[vec] = 1; 
    ^
mytest_vectorhasher.cpp:13:17: note: candidate function not viable: no known conversion 
    from 'const std::__1::vector<unsigned long, std::__1::allocator<unsigned long> >' 
    to 'std::vector<vector<unsigned long, allocator<unsigned long> > > &' for 1st 
    argument 
std::size_t operator()(std::vector<T> &in) const 
      ^
1 error generated. 

難道我搞亂了嗎?我究竟做錯了什麼?

+0

[C++ unordered \ _map使用自定義類類型作爲鍵]的可能重複(http://stackoverflow.com/questions/17016175/c-unordered-map-using-a-custom-class-type-as -the-key) –

回答

1

這個例子中有兩個錯誤。首先,你的哈希器的operator()參數應該是const。

std::size_t operator()(const std::vector<T> &in) const 
//  Add const here ^^^^^ 

二是要定義你的散列類型vectorHasher<std::vector<std::size_t> >其將意味着T = std::vector<std::size_t>,也就是說,您正試圖實例化一個散列器與std::size_t operator()(const std::vector<std::vector<std::size_t>> &in) const是過多的載體。取出std::vector從地圖類型聲明是這樣的:

typedef std::unordered_map< std::vector<std::size_t>, int, vectorHasher<std::size_t> > map_type; 
//      Remove std::vector from this template argument ^^^^^^^^^^^ 
+0

傻了!謝謝! – Seth

1

的問題是你有operator()錯誤的參數。當你構建vectorHasher使用

vectorHasher<std::vector<std::size_t>> 

這意味着T

std::vector<std::size_t> 

這意味着您的operator()看起來像

std::size_t operator()(std::vector<std::vector<std::size_t>> &in) const 

這是不是你想要的。你可以做的是使用T直接像

std::size_t operator()(const T& in) const 

或改變vectorHasher

vectorHasher<std::size_t> 
+0

感謝您的意見。 – Seth

+0

@Seth沒問題。樂意效勞。 – NathanOliver

0

有您的代碼2級的錯誤。

1)既然你定義你的vectorHasher

template <typename T> 
struct vectorHasher{ 
std::size_t operator()(std::vector<T> &in) const 
    { 
    // ... 
    } 
}; 

下面的模板實例vectorHasher<std::vector<std::size_t>>擴展爲:

struct vectorHasher{ 
std::size_t operator()(std::vector<std::vector<std::size_t>> &in) const 
    { 
    // ... 
    } 
}; 

而且,果然,std::vector<std::size_t>不能被傳遞給這個函數。

將模板的實例化更改爲vectorHasher<std::size_t>

2)使用散列的structoperator()需要採取const值/參考,所以你應該它的簽名改爲:

std::size_t operator()(std::vector<T> const &in) const 

Live demo

+0

感謝您的輸入。 – Seth

+0

@Seth不客氣:) –