如何打印元素的地图内部的矢量
How to print element of the map inside the vector?
我想在vector内部的map中打印值和键。
我在互联网上搜索如何打印地图中的元素,但没有找到结果。怎么做呢?
以下是我的代码。
#include <iostream>
#include <string>
#include <map>
using namespace std;
vector<map<string,int>> list;
vector<map<string, int>>::iterator it;
int N;
int M;
int main(void) {
cin >> N;
string s;
int num = 0;
for (int i = 0; i < N; ++i) {
scanf("%s,%d", s, &num);
map<string, int> product;
product.insert(pair<string, int>(s, num));
list.push_back(product);
}
for (it = list.begin(); it != list.end(); ++it) {
//I don't know how to print the elements in the map.
}
}
*it
将包含您的map,因此也要循环遍历这些迭代器
for (it = list.begin(); it != list.end(); ++it) {
for (map<string, int>::iterator mapIt(it->begin()); mapIt != it->end(); ++mapIt) {
// output here
std::cout << mapIt->first << ", " << mapIt->second << std::endl;
}
}
相关文章:
- 为什么不;名字在地图上是按顺序排列的吗
- 将函数类成员映射到类本身内部
- 基于多个条件处理地图中的所有元素
- 在C++中将矢量转换为嵌套地图
- Boost Spirit,获取迭代器内部语义动作
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 内联函数中具有内部链接的全局变量
- 在函数内部的声明中初始化数组,并在外部使用它
- 如何在不知道向量大小的情况下输入向量内部的向量?
- 卷曲bracers内部结构的声明
- 从函数角度看ID到文件路径的内部与外部映射
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- 更新OpenMP内部的哈希地图以进行循环
- DFS 访问内部的地图元素.时间复杂度
- 使用内部灰泥构建器绘制地图
- 如何更改地图容器的内部排序方案?
- 访问地图内部的矢量,该矢量位于另一个地图内
- 如何打印元素的地图内部的矢量
- 结构内部地图上的分割故障
- 计算地图内部的重叠时间