簡體   English   中英

如何通過升壓unordered_map進行迭代?

[英]how to iterate through boost unordered_map?

我只想遍歷無序地圖的成員。

Web上有很多簡單的示例,包括此站點上的示例,但都不會編譯。 顯然,有些示例來自以前的非標准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;
}

大多數錯誤是這種情況的變體:

error: 'template<class K, class T, class H, class P, class A> class boost::unordered::unordered_map' used without template parameters

這是我的構建命令:

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

我因陷入這樣一個初學者的問題而感到尷尬,但是從我發現的大量類似問題中,這足以阻止很多人前進。 我以前曾經寫過哈希容器(建議不要使用STL時),我很想寫自己的東西……但是正確的做法是學習使用盡可能多的現有工具……幫助!

我查看了關於stackoverflow的以下問題,但沒有找到答案:

使用boost_foreach遍歷unordered_map

我試過了:

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

但是它給出了我在下面顯示的相同錯誤。

Unordered_map迭代器無效

這是接近的,但不是我的問題:

boost :: unordered_map中的迭代器無效

這個使用自動,目前我不能切換編譯器。

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

這是語法錯誤。 聲明迭代器時,我將類型放置在錯誤的位置。

感謝大家的回應。

嘗試更改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; ,或者您可以稱之為的任何形式。

答案就在問題中,但是為您提供方便,此處提供了簡單的解決方案:

#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