c++ - Iterator as key-type in an unordered_multimap -


i want create map, uses iterators key-type , integers values, in following example:

#include <list> #include <unordered_map>  int main(int argc, char* argv[]) {   typedef std::list<int> listtype;   typedef std::unordered_multimap<listtype::iterator, unsigned int> maptype;    listtype _list;   maptype _map;    _list.push_back(100);   _map.insert(std::make_pair(_list.begin(), 10));    return 0; } 

unfortunately, makes compiler aborting error c2440: 'conversion' : cannot convert 'const std::_list_iterator<_mylist>' 'size_t'. there can achieve anyway?

the error means have provide hash function particular iterator type. can pass hashing function third template parameter std::unordered_map.

unordered_map requires equality comparator keys, std::list's iterator has that, not need provide own. example:

#include <list> #include <unordered_map> #include <cstddef>  typedef std::list<int> listtype; typedef std::list<int>::iterator listiterator; // poor hashing functor struct myhash {   size_t operator()(const listiterator&) const {     // provide useful implementation here!     size_t hash_ = .... ;     return hash_;     //return 0; // compiles, useless   } };  typedef std::unordered_multimap<listiterator, unsigned int, myhash> maptype;  int main() {    listtype list_;   maptype map_;    list_.push_back(100);   map_.insert(std::make_pair(list_.begin(), 10));    return 0; } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -