0
点赞
收藏
分享

微信扫一扫

将字符串存入map中,并打印输出所有key及value

产品喵dandan米娜 2022-03-11 阅读 178
散列表c++

问题:使用map统计一个字符串中的每个字符出现的次数

#include<iostream>
#include<map>
#include<string>
using namespace std;

int main()
{
	map<char, int>needs;
	string s;
	s = { "asdwafsada" };
	for (auto x : s)
	{
		needs[x]++; //遍历每个字符
	}
	map<char, int>::iterator it;  //创建一个迭代器   
	for (it = needs.begin(); it != needs.end(); it++)  // 这里能不能使用auto这种进行遍历???
	{
		cout << it->first <<'='<< it->second << endl;
	}
	return 0;
}

​

输出结果:

 

举报

相关推荐

0 条评论