c++ - Replace vector and hash table with Boost.Bimap -
i'm looking replace vector<string>
, boost::unordered_map<string, size_t>
mapping string indices in former boost::bimap
.
what instantiation of bimap
should use? far, i've come with
typedef bimap< unordered_set_of<size_t>, vector_of<string> > stringmap;
but i'm not sure if i've reversed collection types now. also, wonder if should change collection of relations type. vector_of_relation
best choice, or set_of_relation
, or go default?
to bimap between size_t , std::string have ~constant (up cost of hashing , potential clashes) need use unordered_set_of:
#include <boost/bimap.hpp> #include <boost/bimap/unordered_set_of.hpp> #include <string> #include <iostream> #include <typeinfo> int main(int argc, char* argv[]) { typedef boost::bimap< boost::bimaps::unordered_set_of<size_t>, boost::bimaps::unordered_set_of<std::string> > stringmap; stringmap map; map.insert(stringmap::value_type(1,std::string("cheese"))); map.insert(stringmap::value_type(2,std::string("cheese2"))); typedef stringmap::left_map::const_iterator const_iter_type; const const_iter_type end = map.left.end(); ( const_iter_type iter = map.left.begin(); iter != end; iter++ ) { std::cout << iter->first << " " << map.left.at(iter->first) << "\n"; } }
returns:
1 cheese 2 cheese2
the unordered_set boost version of set uses hash tables instead of trees store elements, see boost unordered docs.
looking @ comments 1 of bimap examples @ bimap example, have:
the left map view works std::unordered_map< std::string, long >, given name of country can use search population in constant time
Comments
Post a Comment