在地图内配对-如何foreach
pair inside map - how to foreach
我有这个代码:
typedef std::map<const char *, std::pair<const char *, const char *> > MyMap;
MyMap the_map;
the_map.insert(std::make_pair("Text1", std::make_pair("Text2", "Text3")));
显然,目的是以这种方式存储信息:
"Text1"->"Text2"->"Text3"
问题:如何预先处理第一个键的每个元素(如"Text1")并更改每个内部键的值(如"Text3")。
谢谢。
std::对没有begin()
或end()
函数。
你只需要一个循环:
for (const auto& it : the_map) {
std::cout << it.first << " " << it.second.first
<< " " << it.second.second << std::endl;
}
您可以使用映射的迭代器来抛出所有元素:
for (std::map<const char *, std::pair<const char *, const char *> >::iterator ii = the_map.begin(), e = the_map.end(); ii != e; ii++) {
// ii.first - key value
// ii.second - stored value (in your case a pair)
//
// ii.second.first - key value of pair stored in map under ii.first
// ii.second.second - stored value of pair stored in map under ii.first
}
相关文章:
- 如何循环打印顶点结构
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 如何将stdout重定向到stderr
- Mongodb c++驱动程序:如何查询元素的数组
- 当回溯以零开始时,如何调试崩溃
- 如何反转整数参数包
- 如何使用Google Mock来模拟gettimeofday()
- 如何使用默认参数等选择模板专业化
- 如何从C++中的依赖类型中获得它所依赖的类型
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 如何确定我已使用非编码文件到达 EOF?
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何使用ndk-build.cmd构建Android.so文件
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- 循环后如何继续阅读
- 在地图内配对-如何foreach
- 使用 foreach 语法迭代时如何检查我是否在最后一个元素上