c++ - how to iterate through boost unordered_map? -


i want iterate through members of unordered map.

there many simple examples on web, including on site, , yet none of them compile. apparently examples previous non-standard stl version, old, , new gcc 4.7.2 can't handle them. please not suggest new auto iterator c++11. there day when libraries validated that. until then, want old 1 work. (see below have tried)

here test code:

#include <iostream> #include <boost/unordered_map.hpp> #include <string>  int main(int argc,char *argv[]) {     boost::unordered::unordered_map<std::string,int> umap;      //can't gcc accept value_type()...     //umap.insert(boost::unordered_map::value_type("alpha",1));     //umap.insert(boost::unordered_map::value_type("beta",2));     //umap.insert(boost::unordered_map::value_type("gamma",3));      umap["alpha"]=1; //this works     umap["beta"]=2;     umap["gamma"]=3;      //can't gcc compile iterator     //for (boost::unordered_map::iterator it=umap.begin();it!=umap.end();++it)     //  std::cout << it->first <<", " << it->second << std::endl;      //gcc not way either     //for (int x=0;x<umap.size();x++)     //  std::cout << x << " : " << umap[x].first << " = " << umap[x].second << std::endl;      //will gcc take this? no not     //for (int x=0;x<umap.size();x++)     //  std::cout << x << " : " << umap[x] << std::endl;      //this not work     //boost::unordered::unordered_map::iterator<std::string,int> it;      //this not work     //boost::unordered::unordered_map::iterator it;     //for (it=umap.begin();it!=umap.end();++it)     //  std::cout << it->first <<", " << it->second << std::endl;      //this not work     //boost_foreach(boost::unordered_map::value_type value, umap) {     //  std::cout << value.second;     //  }     //std::cout << std::endl;      //this not work either     //boost_foreach(boost::unordered_map::value_type<std::string,int> value, umap) {     //  std::cout << value.second;     //  }     //std::cout << std::endl;      std::cout << "umap size: " << umap.size() << std::endl;     std::cout << "umap max size: " << umap.max_size() << std::endl;     std::cout << "find alpha: " << (umap.find("alpha")!=umap.end()) << std::endl;     std::cout << "count beta: " << umap.count("beta") << std::endl; } 

most of errors variation of this:

error: 'template<class k, class t, class h, class p, class a> class boost::unordered::unordered_map' used without template parameters 

here build command:

g++ -i..\boost umap.cpp 

i should embarrassed getting stuck on such beginner's question, volume of similar questions finding, hard enough stop lot of people in tracks. have written hash containers before (back when recommended not use stl) , tempted write own... right thing learn use many existing tools possible... help!

i've looked @ following questions on stackoverflow haven't found answer:

iterate through unordered_map using boost_foreach

i tried:

boost_foreach(boost::unordered_map::value_type& value, umap) { 

but gives same error show below.

unordered_map iterator invalidation

this 1 close, not quite issue:

iterator invalidation in boost::unordered_map

this 1 uses auto , can't switch compilers @ time.

c++ questions on boost::unordered_map & boost::hash

this 1 theory of maps:

how use boost::unordered_map

this rather complicated example, see in code trying declare iterators... won't compile.

how use boost_foreach unordered_map?

this nice example, not compile. tried version of in code.

it works !

here working code:

#include <iostream> #include <boost/unordered_map.hpp> #include <string>  int main(int argc,char *argv[]) {     boost::unordered::unordered_map<std::string,int> umap;     umap["alpha"]=1;      umap["beta"]=2;     umap["gamma"]=3;     boost::unordered::unordered_map<std::string,int>::iterator it;     (it=umap.begin();it!=umap.end();++it)         std::cout << it->first <<", " << it->second << std::endl;      std::cout << "umap size: " << umap.size() << std::endl;     std::cout << "umap max size: " << umap.max_size() << std::endl;     std::cout << "find alpha: " << (umap.find("alpha")!=umap.end()) << std::endl;     std::cout << "count beta: " << umap.count("beta") << std::endl;     } 

it syntax error. putting type in wrong place when declaring iterator.

thanks responses.

try changing boost::unordered::unordered_map::iterator it; boost::unordered::unordered_map<std::string,int>::iterator it;

note: possible, , idea in more complex situations, create typedef of it, such typedef boost::unordered::unordered_map<std::string,int>::iterator umapstringintit;, or whatever may call it.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -