简体   繁体   English

如何通过升压unordered_map进行迭代?

[英]how to iterate through boost unordered_map?

I just want to iterate through the members of an unordered map. 我只想遍历无序地图的成员。

There are many simple examples on the web, including on this site, and yet none of them will compile. Web上有很多简单的示例,包括此站点上的示例,但都不会编译。 Apparently some examples are from a previous non-standard STL version, some are just old, and some are so new that my gcc 4.7.2 can't handle them. 显然,有些示例来自以前的非标准STL版本,有些只是旧的,有些是如此新,以至于我的gcc 4.7.2无法处理它们。 Please do not suggest the new auto iterator from C++11. 请不要建议使用C ++ 11的新自动迭代器。 I will get there some day when all my libraries are validated for that. 有一天我所有的库都经过验证之后,我会到达那里。 Until then, I just want the old one to work. 在那之前,我只希望旧的工作。 (see below for what I have tried) (有关我的尝试,请参见下文)

Here is my 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 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;
}

Most of the errors are a 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 is my build command: 这是我的构建命令:

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

I should be embarrassed for getting stuck on such a beginner's question, but from the volume of similar questions I am finding, this is just hard enough to stop a lot of people in their tracks. 我因陷入这样一个初学者的问题而感到尴尬,但是从我发现的大量类似问题中,这足以阻止很多人前进。 I have written hash containers before (back when it was recommended NOT to use STL) and I am very tempted to just write my own... but the right thing to do is learn to use as many existing tools as possible... help! 我以前曾经写过哈希容器(建议不要使用STL时),我很想写自己的东西……但是正确的做法是学习使用尽可能多的现有工具……帮助!

I've looked at the following questions on stackoverflow where I haven't found an answer: 我查看了关于stackoverflow的以下问题,但没有找到答案:

iterate through unordered_map using boost_foreach 使用boost_foreach遍历unordered_map

I tried: 我试过了:

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

but it gives the same error I show below. 但是它给出了我在下面显示的相同错误。

Unordered_map iterator invalidation Unordered_map迭代器无效

This one is close, but not quite my issue: 这是接近的,但不是我的问题:

Iterator invalidation in boost::unordered_map boost :: unordered_map中的迭代器无效

This one uses auto and I can't switch compilers at this time. 这个使用自动,目前我不能切换编译器。

C++ some questions on boost::unordered_map & boost::hash C ++关于boost :: unordered_map和boost :: hash的一些问题

This one is mostly about the theory of maps: 这主要是关于地图的理论:

how to use boost::unordered_map 如何使用boost :: unordered_map

This is a rather complicated example, but you will see in my code I am already trying to declare iterators... they just won't compile. 这是一个相当复杂的示例,但是您会在我的代码中看到我已经在尝试声明迭代器了……它们只是无法编译。

How to use BOOST_FOREACH with an Unordered_map? 如何将BOOST_FOREACH与Unordered_map一起使用?

This is a nice example, but it just does not compile. 这是一个很好的例子,但它不能编译。 I tried a version of this in my code. 我在代码中尝试了此版本。

IT WORKS ! 有用 !

Here is the 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;
    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;
    }

It was a syntax error. 这是语法错误。 I was putting the type in the wrong place when declaring the iterator. 声明迭代器时,我将类型放置在错误的位置。

Thanks everyone for your responses. 感谢大家的回应。

try changing boost::unordered::unordered_map::iterator it; 尝试更改boost::unordered::unordered_map::iterator it; it to boost::unordered::unordered_map<std::string,int>::iterator it; 它来boost::unordered::unordered_map<std::string,int>::iterator it;

NOTE: It is also possible, and a good idea in more complex situations, to create a typedef of it, such as typedef boost::unordered::unordered_map<std::string,int>::iterator UMapStringIntIt; 注意:也可以在更复杂的情况下创建它的typedef,例如typedef boost::unordered::unordered_map<std::string,int>::iterator UMapStringIntIt; , or whatever you may call it. ,或者您可以称之为的任何形式。

The answer is in the question, but the simple solution is here for your convenience: 答案就在问题中,但是为您提供方便,此处提供了简单的解决方案:

#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;

    for ( auto it= umap.begin(); it != umap.end(); ++it )
        std::cout << it->first <<", " << it->second << std::endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM