2017-01-12 8 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 - キー) –

答えて

1

この例では2つのエラーがあります。最初に、あなたのハッカーのoperator()パラメータはconstでなければなりません。

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

第二には、あなたはあまりにも多くのベクターであるstd::size_t operator()(const std::vector<std::vector<std::size_t>> &in) constで調理人をインスタンス化しようとしていることを意味し、T = std::vector<std::size_t>ことを示唆するものであろうvectorHasher<std::vector<std::size_t> >としてあなたのハッシュタイプを定義しているです。このようなマップ型宣言から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を構築するとき、あなたはTを意味

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

operator()はあなたが望むものではありませんどの

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

のように見えるあなたの意味

std::vector<std::size_t> 

で使用しています。あなたにできることは、直接

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

ようTを使用するか、あなたのコード内の2個のエラーがあります

vectorHasher<std::size_t> 
+0

ありがとうございました。 – Seth

+0

@Seth問題はありません。喜んで助けてください。 – NathanOliver

0

するvectorHasherを変更することです。案の定

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

そして、std::vector<std::size_t>は、この関数に渡すことはできません。

1)あなたはvectorHasher

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

として、次のテンプレートのインスタンス化 vectorHasher<std::vector<std::size_t>>を定義しているためにに展開されます。

テンプレートのインスタンス化をvectorHasher<std::size_t>に変更します。

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

Live demo:あなたがそれの署名を変更する必要がありますので、ハッシュに使用struct

2)operator()は、const値/参照を取る必要があります。

+0

入力いただきありがとうございます。 – Seth

+0

@Sethあなたは大歓迎です:) –

関連する問題