给定一个string 字符串strArr,和一个char字符c,返回c在strArr中出现的个数。
用一个map<char,int>记录字符和出现的次数。谈不上什么算法,基本就是coding.
1.char c=strArr[i]: //类型转换string->char
2.int len=strArr.length(); //数组长度 strArr.size() 也可以获得长度
3.map<char ,int> word_count; //
赋值word_count[c]++; //map赋值
查值auto it = word_count.find(c);//查key value
上代码
#include <string>
#include <vector>
#include <iostream>
#include <map>
using namespace std;
int getStrNum(string strArr, char c)
{
int m_rc = 0;
// strArr = "abcdefgaaaaccc";
map<char ,int> word_count;
for (int i = 0; i < strArr.length(); i++)
{
char c1 = strArr[i];
word_count[c1]++;
}
for (auto it = word_count.begin(); it != word_count.end(); it++)
{
cout << "[" << it->first << "] = " << it->second << endl;//iter->first:key,iter->second:value
}
cout << "******************" << endl;
auto it1 = word_count.find(c);
cout << "[" << it1->first << "] = " << it1->second << endl;
m_rc = it1->second;
return m_rc;
}
原文链接: https://www.cnblogs.com/jasmineTang/p/14369299.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/327464
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!