2017-01-15 46 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; 
     }); 
    }); 
相关问题