【解决方法一】C++ 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>(, "student_one"));
mapStudent.insert(pair<int, string>(, "student_two"));
mapStudent.insert(pair<int, string>(, "student_three")); map<int, string>::iterator iter;
iter = mapStudent.find();
if (iter != mapStudent.end())
cout << "Find,the value is: " << iter->second << endl;
system("pause");
return ;
}
运行结果:
二、map的插入方法:
插入的方法有好几种,下面介绍:map.insert(pair<type1,type2>(key,value))这种,还有一种是map[key] = value,前者出现重复不会发生改变,后者出现重复则会发生覆盖,后者如果没有给value值,直接使用map[key],则其value值默认为0。示例代码:
#include<iostream>
#include<string>
#include<map>
using namespace std; int main() { map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(, "student_one"));
mapStudent.insert(pair<int, string>(, "student_two"));
mapStudent.insert(pair<int, string>(, "xxooxxooxxo"));//不起作用 map<int, string>::iterator iter;
for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++) {
cout << iter->first << ' ' << iter->second << endl;
}
cout << endl;
mapStudent[] = "xxooxxooxxoo";//覆盖掉前面的value
for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++) {
cout << iter->first << ' ' << iter->second << endl;
}
system("pause");
return ;
}
运行结果:
三、是否插入成功?
我们通过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:先创建一个map,遍历字符串,逐个判断如果存在则count++,不存在则创建一个,让其value为1。代码如下:
#include<iostream>
#include<string>
#include<map>
using namespace std; // 方法1:
int main()
{
map<char, int> map_s;
string str = "kkk ahguird-j l"; for (int i = ; i < str.length(); ++i)
{
map<char, int>::iterator iter = map_s.find(str[i]);
if (iter != map_s.end())
{
iter->second++;
}
else // 如果找不到就添加一个,找到了就count++
{
map_s.insert(pair<char, int>(str[i], )); // 如果测试用例为 "qwerqwer"时,前4次循环都是执行的这句else即insert插入操作
}
}
map<char, int>::iterator iter = map_s.begin(); for (; iter != map_s.end(); iter++)
{
cout << iter->first << ' ' << iter->second << endl;
}
cout << endl; system("pause");
return ;
}
运行结果:
思路2:通过插入失败来增加字符的个数,如果插入失败则表明map中存在该字符,count++即可。代码如下:
#include<iostream>
#include<string>
#include<map>
using namespace std; int main()
{
map<char, int> map_s;
string str = "kkk ahguird-j l"; pair<map<char, int>::iterator, bool> ret;
for (int i = ; i < str.length(); ++i)
{
ret = map_s.insert(pair<char, int>(str[i], ));
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; system("pause");
return ;
}
运行结果:
思路3:直接使用库里提供的[]运算符重载。通过键值找节点,直接给给实值+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 = ; 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.****.net/qq_34312386/article/details/55281662
对思路3补充说明:假设一个map对象map_s中只存在一对pair<char,int>('a',1),现在执行map_s['b']则map_s中存在两对pair分别是('a',1),('b',0),是的,没错,'b'的value默认是0,那么如果刚才执行的不是map_s['b'],而是map_s['b']++,那么map_s中的两对pair为('a',1)和('b',1),理解为插入‘b’,然后value值++,所以,map_s['b']++这个语句的理解如下:
如果map_s中不存在key为‘b’的元素,那么在map_s中添加‘b’,value默认为0,然后map_s['b']++,value变成1.
如果map_s中存在key为 'b' 的元素,那么该语句表示map_s['b']的value++.
#include<iostream>
#include<map>
using namespace std; int main() {
map<char, int> m;
m.insert(pair<char, int>('a', ));
map<char, int>::iterator iter = m.begin();
cout << iter->first << ' '<<iter->second<<endl;
m['b'];
iter++;
cout << iter->first << ' ' << iter->second << endl;//value默认是0
m['b']++;
cout << iter->first << ' ' << iter->second << endl;//将value++
while ();
}