题解 | #C++计算某字符出现次数#
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
#include <iostream> #include <string> #include<unordered_set> using namespace std; int main() { string s;//定义字符串变量s接收输入 getline(cin, s);////使用getline读取一行字符串 char c;//定义变量c接收单个字符输入 cin>>c;//使用cin读取一个字符入c //使用无序关联容器unordered_multiset,可以存储重复元素 unordered_multiset<char> set; for (char i : s) {//遍历字符串s中的每个字符 //tolower转换字符忽略大小写 set.insert(tolower(i));//将字符转换为小写后插入set } //使用set的count方法统计c在set中出现的次数 int n=set.count(tolower(c)); cout << n << endl; }#刷题#