如何迭代boost unordered_map?

use*_*315 0 c++ boost

我只想迭代无序地图的成员.

网上有很多简单的例子,包括在这个网站上,但是没有一个会编译.显然,一些例子来自以前的非标准STL版本,有些只是旧的,有些是如此新,以至于我的gcc 4.7.2无法处理它们.请不要建议使用C++ 11中的新自动迭代器.有一天,当我的所有库都经过验证时,我会到达那里.在那之前,我只想让旧的工作.(见下面我试过的)

这是我的测试代码:

#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 get gcc to accept the 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 get gcc to compile the iterator
    //for (boost::unordered_map::iterator it=umap.begin();it!=umap.end();++it)
    //  std::cout << it->first <<", " << it->second << std::endl;

    //gcc does not like it this 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 it does not
    //for (int x=0;x<umap.size();x++)
    //  std::cout << x << " : " << umap[x] << std::endl;

    //this does not work
    //boost::unordered::unordered_map::iterator<std::string,int> it;

    //this does 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 does not work
    //BOOST_FOREACH(boost::unordered_map::value_type value, umap) {
    //  std::cout << value.second;
    //  }
    //std::cout << std::endl;

    //this does 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;
}
Run Code Online (Sandbox Code Playgroud)

大多数错误都是这种变化:

error: 'template<class K, class T, class H, class P, class A> class boost::unordered::unordered_map' used without template parameters
Run Code Online (Sandbox Code Playgroud)

这是我的构建命令:

g++ -I..\boost umap.cpp
Run Code Online (Sandbox Code Playgroud)

我会因为陷入这样一个初学者的问题而感到尴尬,但是从我发现的类似问题的数量来看,这很难阻止很多人的追踪.我以前写过哈希容器(当时建议不要使用STL)我很想写自己的...但是正确的做法是学会尽可能多地使用现有工具...帮助!

我在stackoverflow上查看了以下问题,但我没有找到答案:

使用boost_foreach迭代unordered_map

我试过了:

BOOST_FOREACH(boost::unordered_map::value_type& value, umap) {
Run Code Online (Sandbox Code Playgroud)

但它给出了我在下面显示的相同错误.

Unordered_map迭代器失效

这个很接近,但不是我的问题:

boost :: unordered_map中的迭代器失效

这个使用auto,我此时无法切换编译器.

C++关于boost :: unordered_map和boost :: hash的一些问题

这个主要是关于地图理论:

如何使用boost :: unordered_map

这是一个相当复杂的例子,但你会在我的代码中看到我已经在尝试声明迭代器......它们只是不会编译.

如何将BOOST_FOREACH与Unordered_map一起使用?

这是一个很好的例子,但它只是不编译.我在我的代码中尝试了这个版本.

有用 !

这是工作代码:

#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;
    for (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;
    }
Run Code Online (Sandbox Code Playgroud)

这是一个语法错误.在声明迭代器时,我将类型放在错误的位置.

感谢大家的回应.

Kev*_*vin 8

尝试将boost::unordered::unordered_map::iterator it;其更改为boost::unordered::unordered_map<std::string,int>::iterator it;

注意:在更复杂的情况下,也可以创建一个typedef,例如typedef boost::unordered::unordered_map<std::string,int>::iterator UMapStringIntIt;,或者你可以称之为的任何东西.

  • 值得指出的是"错误:'`模板<类K,类T,类H,类P,类A>类boost :: unordered :: unordered_map`'无模板参数使用"表达了这个......模板参数需要的是`<std :: string,int>`. (2认同)