c++里map<string,string>怎么遍历

我尝试了下map<int,string>是可以用iterator遍历的,但是<string,string>就不行... 我尝试了下map<int,string>是可以用iterator遍历的,但是<string,string>就不行 展开
 我来答
流禅水7884
2014-06-03 · TA获得超过110个赞
知道答主
回答量:116
采纳率:0%
帮助的人:113万
展开全部
这个是不是map里面的string参数? 是的
it->second是map中对应于it->first的vector, 你这样的写法导致了复制,应该用引用。
tmp[i] 是node变量。

下面是一段简化的代码:

#include <map>
#include <vector>
#include <iostream>

struct Point {
int x;
int y;
};

std::ostream & operator <<(std::ostream & out, const Point & p) {
return out << '(' << p.x << ',' << p.y << ')';
}

using ConType = std::map<std::string, std::vector<Point>>;

void travel(ConType & con);

int main() {
std::map<std::string, std::vector<Point>> con;
std::vector<Point> a, b, c;
a.push_back({1, 3});
a.push_back({4, 5});
a.push_back({5, 7});
b.push_back({2, 3});
b.push_back({5, 3});
c.push_back({5, 7});
c.push_back({5, 4});
con["a"] = a;
con["b"] = b;
con["c"] = c;
travel(con);
}

void travel(ConType & con) {
for(auto & i : con) {
for(auto & j : i.second) {
std::cout << j << '\t';
}
std::cout << std::endl;
}
}
dotaer603
2019-05-23
知道答主
回答量:21
采纳率:0%
帮助的人:1.7万
展开全部
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<string,string> m;
m.insert(pair<string, string>("小红","1班"));
m.insert(pair<string, string>("小明", "2班"));
m.insert(pair<string, string>("小黄", "3班"));
for (map<string, string>::iterator it = m.begin(); it != m.end(); it++) {
cout << it->first <<"\t"<<m[it->first] << endl;
}
return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
掌如曼o1
2020-10-27
知道答主
回答量:8
采纳率:0%
帮助的人:2933
展开全部

遍历方法如下:

map<string,string> mymap;

auto it = mymap.begin();

for(;it != mymap.end();++it)

{

cout << it->first.c_str() << " " << it->second.c_str() << endl;

}


已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式