使用map统计字符出现次数

一、map中的find函数:

用于查找map中是否包含某个关键字条目,传入的参数是要查找的key,最后返回一个迭代器,如果没有找到,则返回的迭代器等于end()返回的迭代器。

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

int main() {
	map<int, string> mapStudent;
	mapStudent.insert(pair<int, string>(1, "student_one"));
	mapStudent.insert(pair<int, string>(2, "student_two"));
	mapStudent.insert(pair<int, string>(3, "student_three"));
	map<int, string>::iterator iter;
	iter = mapStudent.find(3);
	if (iter != mapStudent.end())//只要返回的不是end()的迭代器表示已经找到了
		cout << "Find,the value is: " << iter->second << endl;
	while (1);
}

 二、map的插入方法:

主要有四种,参考:https://blog.csdn.net/F_Z_M/article/details/78119514

主要使用:map.insert(pair<type1,type2>(key,value))这种,还有一种是map[key] = value,前者出现重复不会发生改变,后者出现重复则会发生覆盖

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

int main() {
	map<int, string> mapStudent;
	mapStudent.insert(pair<int, string>(1, "student_one"));
	mapStudent.insert(pair<int, string>(2, "student_two"));
	mapStudent.insert(pair<int, string>(2, "xxooxxooxxo"));//不起作用
	map<int, string>::iterator iter;
	for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++) {
		cout << iter->first << ' ' << iter->second<<endl;
	}
	cout << endl;
	mapStudent[2] = "xxooxxooxxoo";//覆盖掉前面的value
	for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++) {
		cout << iter->first << ' ' << iter->second << endl;
	}
	while (1);
}

三、是否插入成功?

我们通过pair来获取是否插入成功,pair返回两个变量,第一个是map的迭代器,第二个是插入成功的标志,插入成功pair的第二个参数是true,插入失败,第二个参数为false.

pair<map<type1,type2>::iterator,bool> ret;

ret = map_s.insert(pair<type1,type2>(key,value))

if(ret.second==ture)

    cout<<"插入成功"<<endl;
else
    cout<<"插入失败"<<endl;

 四、统计字符出现个数:

思路1:先创建一个map,遍历字符串,逐个判断如果存在则count++,不存在则创建一个,让其value为1

思路2:通过插入失败来增加字符的个数,如果插入失败则表明map中存在该字符,count++即可

思路3:需要对map了解,直接使用库里提供的[]运算符重载。通过键值找节点,直接给给实值+1.

思路1:

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

int main() {
	string str;
	map<char, int> map_s;
	while (cin >> str) {
		for (int i = 0; i < str.length(); ++i) {
			map<char, int>::iterator iter = map_s.find(str[i]);
			if (iter != map_s.end()) {
				iter->second++;
			}
			else {
				map_s.insert(pair<char, int>(str[i], 1));//如果找不到就添加一个,找到了就count++
			}
		}
		map<char, int>::iterator iter = map_s.begin();
		for (; iter != map_s.end(); iter++) {
			cout << iter->first << ' ' << iter->second << endl;
		}
		cout << endl;
	}
}

思路2:

扫描二维码关注公众号,回复: 5648441 查看本文章
#include<iostream>
#include<string>
#include<map>
using namespace std;

int main() {
	string str;
	map<char, int> map_s;
	while (cin >> str) {
		pair<map<char, int>::iterator, bool> ret;
		for (int i = 0; i < str.length(); ++i) {
			ret = map_s.insert(pair<char, int>(str[i], 1));
			if (ret.second == false) {//如果插入失败,则该迭代器的第一个参数的value++
				ret.first->second++;
			}
		}
		map<char, int>::iterator iter = map_s.begin();
		for (; iter != map_s.end(); iter++) {
			cout << iter->first << ' ' << iter->second << endl;
		}
		cout << endl;
			
	}
}

 思路3:

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

int main() {
	string str;
	map<char, int> map_s;
	while (cin >> str) {
		for (int i = 0; i < str.length(); ++i) {
			map_s[str[i]]++;
		}
		map<char, int>::iterator iter;
		for (iter = map_s.begin(); iter != map_s.end(); ++iter) {
			cout << iter->first << ':' << iter->second << endl;
		}
	}
}

思路3是真的6阿~

参考连接:https://blog.csdn.net/qq_34312386/article/details/55281662

猜你喜欢

转载自blog.csdn.net/Li_haiyu/article/details/88782182