我如何循环遍历 C++地图的地图?
如何在 C++ 中循环遍历 std::map
?我的地图定义为:
std::map< std::string, std::map<std::string, std::string> >
例如,上面的容器保存如下数据:
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 a std::map
in C++? 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以使用迭代器。
You can use an iterator.
或者在 C++0x 中更好:
or nicer in C++0x:
使用 C++17(或更高版本),您可以使用“结构化绑定”功能,该功能允许您使用单个元组/对定义具有不同名称的多个变量。示例:
原始提案(由杰出人物 Bjarne Stroustrup、Herb Sutter 和 Gabriel Dos Reis)读起来很有趣(恕我直言,建议的语法更直观);还有标准的拟议措辞 读起来很无聊,但更接近实际内容。
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. Example:
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); there's also the proposed wording for the standard which is boring to read but is closer to what will actually go in.
做这样的事情:
Do something like this:
C++11:
输出:
C++11:
output:
正如einpoklum在他们的答案中提到的,自从C++17 您还可以使用 结构化绑定声明。我想通过提供一个完整的解决方案来扩展这一点,以一种舒适的方式迭代地图的地图:
输出:
注释 1: 为了填充地图,我使用了 初始化列表(这是一个 C++ 11 功能)。有时这可以方便地保持固定初始化的紧凑性。
注2:如果要在循环内修改映射
m
,则必须删除const
关键字。Coliru 上的代码
As einpoklum mentioned in their answer, since C++17 you can also use structured binding declarations. I want to extend on that by providing a full solution for iterating over a map of maps in a comfortable way:
Output:
Note 1: For filling the map, I used an initializer list (which is a C++11 feature). This can sometimes be handy to keep fixed initializations compact.
Note 2: If you want to modify the map
m
within the loops, you have to remove theconst
keywords.Code on Coliru
使用 std::map< std::string、std::map>::const_iterator 当映射是 const 时。
use
std::map< std::string, std::map<std::string, std::string> >::const_iterator
when map is const.第一个解决方案是使用 range_based for 循环,例如:
注意:当
range_expression
的类型为std::map
时,range_declaration
的类型为std::pair
。代码1:
第二种解决方案:
代码2
First solution is Use range_based for loop, like:
Note: When
range_expression
’s type isstd::map
then arange_declaration
’s type isstd::pair
.Code 1:
The Second Solution:
Code 2
老问题,但其余答案从 C++11 开始已经过时 - 您可以使用 基于范围的 for 循环 并简单地执行:
这应该比早期版本干净得多,并避免不必要的复制。
有些人赞成用引用变量的显式定义替换注释(如果未使用,则会被优化):
C++17 更新:现在可以使用结构化绑定进一步简化这一点,如下所示:
Old question but the remaining answers are outdated as of C++11 - you can use a ranged based for loop and simply do:
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):
Update for C++17: it is now possible to simplify this even further using structured bindings, as follows: