Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a std::tuple as key for std::unordered_map

With the code below, I get a very confusing error in MSVC that seems to suggest the key type (an std::tuple) is being converted to an std::string.

#include <iostream>
#include <string>
#include <tuple>
#include <utility>
#include <unordered_map>

typedef std::tuple<std::string,int,char> key_t;

struct key_hash : public std::unary_function<key_t, std::size_t>
{
   std::size_t operator()(const key_t& k) const
   {
      return std::get<0>(k)[0] ^ std::get<1>(k) ^ std::get<2>(k);
   }
};

struct key_equal : public std::binary_function<key_t, key_t, bool>
{
   bool operator()(const key_t& v0, const key_t& v1) const
   {
      return (
               std::get<0>(v0) == std::get<0>(v1) &&
               std::get<1>(v0) == std::get<1>(v1) &&
               std::get<2>(v0) == std::get<2>(v1)
             );
   }
};

struct data
{
   std::string x;
};

typedef std::unordered_map<key_t,data,key_hash,key_equal> map_t;


int main()
{
   map_t m;
   data d;
   d.x = "test data";
   m[std::make_tuple("abc",1,'X')] = d;
   auto itr = m.find(std::make_tuple(std::string("abc"),1,'X'));
   if (m.end() != itr)
   {
      std::cout << "x: " << itr->second.x;
   }
   return 0;
}

Error:

Error   1   error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from 'const std::tr1::tuple<_Arg0,_Arg1,_Arg2>' to 'const std::basic_string<_Elem,_Traits,_Ax> &'  c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\tuple    127 1   

Compiler: MS Visual Studio 2010

On ideone, I get the following even more convoluted error:

http://ideone.com/yEv2j

I can't seem to figure out where I've gone wrong.

like image 798
Gerdiner Avatar asked Jul 10 '12 07:07

Gerdiner


People also ask

Can tuple be key in unordered_map?

Unordered Map does not contain a hash function for a tuple. So if we want to hash a tuple then we have to explicitly provide it with a hash function that can hash a tuple. unordered_map can take up to 5 arguments: Key: Type of key values.

Can you hash a tuple in C++?

The answer is no, can't be done for all tuples, since a tuple is a variadic template type.

Is an unordered set of tuples?

An unordered set of tuples is an unordered set in which each of the elements is a tuple. Note that by default an unordered set doesn't have the functionality of tuples. In simple words, one cannot declare an unordered set of tuples directly in C++. One have to pass a Hash function as an argument to the unordered set.

Does unordered_map store the key?

unordered_map is a data structure that is used to store data in the form of pairs of keys and their corresponding values. Unordered_map uses a hashing function to store a key-value pair, due to which the average time complexity for searching a key-value pair becomes O(1).


2 Answers

The problem for ideone is that key_t already exists:

prog.cpp:7:42: error: conflicting declaration 'typedef class std::tuple<std::basic_string<char>, int, char> key_t'
/usr/include/sys/types.h:123:17: error: 'key_t' has a previous declaration as 'typedef __key_t key_t'

Rename your key_t to something else, or put it into some namespaces.

Your code works after this change in both g++ and clang++. I believe this is a bug in MSVC.

like image 95
kennytm Avatar answered Oct 27 '22 14:10

kennytm


Strange. Your code works fine in Visual Studio 2012 RC and output is "x: test data".

like image 32
ForEveR Avatar answered Oct 27 '22 14:10

ForEveR