2017-01-15 45 views
0

我對如何計算在std :: for_each中使用lambda函數需要什麼類型感到困惑。看來我不能在這種情況下使用自動作爲參數(無論如何Visual Studio 2013抱怨)。如何計算std :: for_each lambda函數所需的類型

在下面我還以爲我會用部分作爲的for_each函數的類型,但其實我不得不使用

const std::pair<std::string, std::unordered_map<std::string, std::string> > 

而且在這種情況下,有什麼做的「內部」類型的代碼我用?

我的主要問題是我如何解決使用什麼類型?

#include <iostream> 
#include <string> 
#include <unordered_map> 
#include <algorithm> 


// key=value pairs within a section 
typedef std::unordered_map<std::string, std::string> keyvalue; 

// [section1] - top level 
typedef std::unordered_map< std::string, std::unordered_map<std::string, std::string> > sections; 

class config 
{ 
public: 
    config() { 
     setup(); 
    } 
    typedef sections::iterator iterator; 
    iterator begin() { return sections_.begin(); } 
    iterator end() { return sections_.end(); } 


private: 
    sections sections_; 

    void setup() { 
     // obviously we wouldn't hard code like this in a real program 
     sections_["programming languages"].insert(std::make_pair("C", "imperative")); 
     sections_["programming languages"].insert(std::make_pair("C++", "OOP")); 
     sections_["programming languages"].insert(std::make_pair("Java", "OOP")); 
     sections_["programming languages"].insert(std::make_pair("Haskell", "functional")); 
     sections_["programming languages"].insert(std::make_pair("Prolog", "logic")); 
    } 
}; 


int main() { 
    config cfg; 
    std::for_each(cfg.begin(), cfg.end(), [](const std::pair<std::string, std::unordered_map<std::string, std::string> > sec) { 
     std::cout << "section name: " << sec.first << std::endl; 

     // what is inner type - thought it would be keyvalue ??? 
     //std::for_each(sec.second.begin(), sec.second.end(), [](const keyvalue& pr) { 
      //std::cout << "first: " << pr << std::endl; 
     //}); 
    }); 

    // I thought type would be sections ??? 
    //std::for_each(cfg.begin(), cfg.end(), [](const sections& sec) { 
    // std::cout << "section name: " << sec.first << std::endl; 
    //}); 
} 
+0

你爲什麼不使用'爲(自動&&秒:CFG)' ? – JVApen

回答

0

您還可以使用decltype用於此目的是這樣的:

config cfg; 
    std::for_each(cfg.begin(), cfg.end(), [](const decltype(*cfg.begin())& sec) { 
     std::cout << "section name: " << sec.first << std::endl; 

     std::for_each(sec.second.begin(), sec.second.end(), [](const decltype(*sec.second.begin())& pr) { 
     std::cout << "first: " << pr.first << std::endl; 
     }); 
    }); 

我不是100%肯定的引用類型是如何影響這一點,所以刪除它可能更適合像const std::remove_reference<decltype(*cfg.begin())>::type& sec

雖然需要注意的是VS2013 ItelliSense在decltype方面有問題,即使它編譯得很好,也會將其標記爲錯誤。

STL容器總是定義一個value_type所以在這種情況下,你可以使用decltype(inst)::value_type,你不需要remove_reference shenanigans。所以,我勸你也value_type類型定義添加到您的類型是這樣的:

class config 
{ 
public: 
    typedef sections::value_type value_type; 

然後,你可以這樣做:

config cfg; 
    std::for_each(cfg.begin(), cfg.end(), [](const decltype(cfg)::value_type& sec) { 
     std::cout << "section name: " << sec.first << std::endl; 

     std::for_each(sec.second.begin(), sec.second.end(), [](const decltype(sec.second)::value_type& pr) { 
     std::cout << "first: " << pr.first << std::endl; 
     }); 
    }); 
相關問題