如何遍历C ++地图?

本文翻译自:How can I loop through a C++ map of maps?

How can I loop through a std::map in C++? 如何在C ++中遍历std::map My map is defined as: 我的地图定义为:

std::map< std::string, std::map<std::string, std::string> >

For example, the above container holds data like this: 例如,上述容器保存如下数据:

m["name1"]["value1"] = "data1";
m["name1"]["value2"] = "data2";
m["name2"]["value1"] = "data1";
m["name2"]["value2"] = "data2";
m["name3"]["value1"] = "data1";
m["name3"]["value2"] = "data2";

How can I loop through this map and access the various values? 如何遍历此映射并访问各种值?


#1楼

参考:https://stackoom.com/question/kknk/如何遍历C-地图


#2楼

Old question but the remaining answers are outdated as of C++11 - you can use a ranged based for loop and simply do: 旧问题,但其余答案从C ++ 11开始已过时-您可以使用基于范围的for循环 ,只需执行以下操作:

std::map<std::string, std::map<std::string, std::string>> mymap;

for(auto const &ent1 : mymap) {
  // ent1.first is the first key
  for(auto const &ent2 : ent1.second) {
    // ent2.first is the second key
    // ent2.second is the data
  }
}

this should be much cleaner than the earlier versions, and avoids unnecessary copies. 这应该比早期版本干净得多,并避免了不必要的复制。

Some favour replacing the comments with explicit definitions of reference variables (which get optimised away if unused): 一些人赞成用引用变量的显式定义替换注释(如果未使用,则将被优化):

for(auto const &ent1 : mymap) {
  auto const &outer_key = ent1.first;
  auto const &inner_map = ent1.second;
  for(auto const &ent2 : inner_map) {
    auto const &inner_key   = ent2.first;
    auto const &inner_value = ent2.second;
  }
}

#3楼

std::map< std::string, std::map<std::string, std::string> >::const_iterator图为const时std::map< std::string, std::map<std::string, std::string> >::const_iterator使用std::map< std::string, std::map<std::string, std::string> >::const_iterator


#4楼

C++11: C ++ 11:

std::map< std::string, std::map<std::string, std::string> > m;
m["name1"]["value1"] = "data1";
m["name1"]["value2"] = "data2";
m["name2"]["value1"] = "data1";
m["name2"]["value2"] = "data2";
m["name3"]["value1"] = "data1";
m["name3"]["value2"] = "data2";

for (auto i : m)
    for (auto j : i.second)
        cout << i.first.c_str() << ":" << j.first.c_str() << ":" << j.second.c_str() << endl;

output: 输出:

name1:value1:data1
name1:value2:data2
name2:value1:data1
name2:value2:data2
name3:value1:data1
name3:value2:data2

#5楼

With C++17 (or later), you can use the "structured bindings" feature, which lets you define multiple variables, with different names, using a single tuple/pair. 在C ++ 17(或更高版本)中,可以使用“结构化绑定”功能,该功能使您可以使用单个元组/对来定义具有不同名称的多个变量。 Example: 例:

for (const auto& [name, description] : planet_descriptions) {
    std::cout << "Planet " << name << ":\n" << description << "\n\n";
}

The original proposal (by luminaries Bjarne Stroustrup, Herb Sutter and Gabriel Dos Reis) is fun to read (and the suggested syntax is more intuitive IMHO); 最初的建议 (由知名人士Bjarne Stroustrup,Herb Sutter和Gabriel Dos Reis提出)很有趣(建议的语法更直观,恕我直言); there's also the proposed wording for the standard which is boring to read but is closer to what will actually go in. 该标准还有一些建议的措词 ,虽然阅读起来很无聊,但却更接近实际内容。


#6楼

You can use an iterator. 您可以使用迭代器。

typedef std::map<std::string, std::map<std::string, std::string>>::iterator it_type;
for(it_type iterator = m.begin(); iterator != m.end(); iterator++) {
    // iterator->first = key
    // iterator->second = value
    // Repeat if you also want to iterate through the second map.
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值