c++中map遍历_怎么遍历map集合
2023-06-13 09:11:13 时间
1、iterator(迭代器)
#include <map>
#include <iostream>
//...
std::map<int, string> m;
//...
for (auto it = m.begin(); it != m.end(); ++it) {
//key
std::cout << it->first << std::endl;
//value
std::cout << it->second << std::endl;
}
2、range for(范围for语句),c++ ver >= 11
#include <map>
#include <iostream>
//...
std::map<int, string> m;
//...
for (const auto &value : m) {
//value
std::cout << value << std::endl;
}
3、structured binding declaration && range for(结构化绑定声明 && 范围for语句) ,c++ver >= 17
#include <map>
#include <iostream>
//...
std::map<int, string> m;
//...
for (const auto &[key, value] : m) {
//key
std::cout << key << std::endl;
//value
std::cout << value << std::endl;
}
1、只取key
#include <map>
#include <iostream>
#include <functional>
//...
std::map<int, string> m;
//...
using namespace std::placeholder;
for (const auto &[key, _1] : m) {
//key
std::cout << key << std::endl;
}
2、只取value
#include <map>
#include <iostream>
#include <functional>
//...
std::map<int, string> m;
//...
using namespace std::placeholder;
for (const auto &[_1, value] : m) {
//value
std::cout << value << std::endl;
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/193246.html原文链接:https://javaforall.cn
相关文章
- C++学习——c++逗号操作符说明(附加全部运算符优先级)
- C++运算符重载和函数重载
- 终极 C++避坑指南
- MAP遍历_map容器遍历
- c++ auto类型_auto C++
- 有序的Map集合_map集合特点
- c++ map是有序还是无序的_实现有序map之go「建议收藏」
- unordered_map/unordered_set/unorder_multiset/unorder_multimap-C++容器
- [C++STL教程]4.map超强的容器,它终于来了!零基础都能理解的入门教程
- 【C++】map 和 set
- 本地推理,单机运行,MacM1芯片系统基于大语言模型C++版本LLaMA部署“本地版”的ChatGPT
- C++ array获取(访问)元素
- C++ array元素的比较
- C++ map(STL map)删除元素(erase函数删除元素)详解
- C++ unordered_map及其基本结构和特性
- C++ unordered_map获取(访问)元素详解
- C++ STL map删除键值对:erase()和clear()方法
- C++ cin判断输入结束(读取结束)
- 深入浅出Redis查看Map(redis查看map)
- 基于Redis集群的Map数据结构的删除(redis集群map删除)
- Redis中计时的Map超时的挑战(redis 超时的map)
- c++中const的使用详解
- 标准C++类string的Copy-On-Write技术
- C++中可正确获取UTF-8字符长度的函数分享
- Lua和C++的通信流程分解