如何自动查找字符串中字符存在的次数
How to automatically find how many times a character existed in a string?
我是 c++ 的初学者,我做了一个代码提到字符串中存在字符的次数:-
#include <string>
#include <iostream>
using namespace std;
int main(){
int s=0;
int l=0;
int o=0;
int w=0;
int y=0;
string boo="slowly";
for (size_t j = 0; j < boo.size(); j++) {
while (boo[j] == 's') {
s=s+1;
break;}
while (boo[j] == 'l')
{
l=l+1;
break;}
while (boo[j] == 'o') {
o=o+1;
break;}
while (boo[j] == 'w') {
w=w+1;
break;}
while (boo[j] == 'y') {
y=y+1;
break;}}
cout <<"s ="<<s<<endl;
cout <<"l ="<<l<<endl;
cout <<"o ="<<o<<endl;
cout <<"w ="<<w<<endl;
cout <<"y="<<y<<endl;
system("pause");
return 0;
}
我想知道如何制作一个代码来自动检测字符串中的字符并对其应用条件,而无需为每个字母字符制作 while 循环并为每个字符制作 int 变量?
**请原谅我的坏英语
您可以使用地图来跟踪不同的字符计数。您循环访问一次输入,然后针对每个字符在地图中查找它。如果地图中尚不存在它,则将创建一个值为 0 的新条目。
创建地图后,每个条目都以字符为键,出现次数作为值,循环访问地图并打印每个条目。
#include <iostream>
#include <map>
#include <string>
int main()
{
std::string input = "slowly";
std::map<char, int> occurrences;
for (char character : input)
{
occurrences[character] += 1;
}
for (auto& entry : occurrences)
{
std::cout << entry.first << '=' << entry.second << std::endl;
}
}
试试这个:
#include <string>
#include <iostream>
using namespace std;
int main(){
int s=0,l=0,o=0,w=0,y=0;
string boo="slowly";
for (int j = 0; j < boo.size(); j++) {
switch(boo.charAt(j)){
case 's':
s++;
break;
case 'l':
l++;
break;
case 'o':
o++;
break;
case 'w':
w++;
break;
case 'y':
y++;
break;
}
}
cout <<"s ="<<s<<endl;
cout <<"l ="<<l<<endl;
cout <<"o ="<<o<<endl;
cout <<"w ="<<w<<endl;
cout <<"y="<<y<<endl;
system("pause");
return 0;
}
如果您的编译器支持 C++ 2011,那么您可以按以下方式编写,前提是字母将按照它们在字符串中出现的顺序列出
#include <iostream>
#include <string>
#include <map>
int main()
{
std::string s( "slowly" );
auto comp = [&]( char c1, char c2 ) { return s.find( c1 ) < s.find( c2 ); };
std::map<char, int, decltype( comp )> m( comp );
for ( char c : s ) ++m[c];
for ( auto p : m ) std::cout << p.first << ": " << p.second << std::endl;
}
程序输出为
s: 1
l: 2
o: 1
w: 1
y: 1
否则,如果编译器不支持 C++ 2011,则必须使用在 main 之前定义的函数类而不是 lambda。此外,youi 应该将基于范围的 for 循环替换为普通循环。
使用一个小技巧。首先,观察所有小写字符都按升序使用数字代码编码:
ASCII EBCDIC
'a' - 97 129
'b' - 98 130
'c' - 99 131
...
'z' - 122 169
代码可能不是连续的,就像 EBCDIC 的情况一样,但这并不重要:创建一个足够大小的int
数组,遍历您的字符串,并为每个小写字符ch
标记索引ch-'a'
。表达式表示字符代码与字母表开头之间的距离。
现在你可以遍历 int
s 的数组,并通过执行索引i
的逆平移来打印相应的字符:
char ch = 'a'+i;
下面是一个完整的示例:
string s;
getline(cin, s);
int seen['z'-'a'+1] {0};
for (int i = 0 ; i != s.size() ; i++) {
char ch = s[i];
if (!islower(ch))
continue;
seen[ch-'a']++;
}
for (int i = 0 ; i != 'z'-'a'+1 ; i++) {
if (seen[i]) {
cout << (char)('a'+i) << " - " << seen[i] << endl;
}
}
演示。
我想STL的算法头会帮助你:
std::string boo="slowly";
std::cout << "s = " << std::count(boo.cbegin(), boo.cend(), 's') << std::endl;
相关文章:
- C++模板来检查友元函数的存在
- C++字符*缓冲区的大小
- HEX值到wchar_t字符(UTF-8)的转换
- 为什么 Serial.println(<char[]>);返回随机字符?
- 我遇到了这个代码片段,不明白. 它递归检查 C++ 字符串中是否存在大写字符
- 从文本文件中读取时,即使不存在,也会找到额外的字符(例如"@")?
- 除了字符串或宏调用之外,是否存在任何情况,其中"(<"是有效的字符流?
- 查找字符串中存在的不同字符
- std::string 上的 substr 无法正常工作,因为存在一些不可见但看起来像空格的字符
- 用“ strcpy”在2D数组中删除(字符)的一行(字符)时是否存在任何错误
- C++ 路径文件存在限制为 260 个字符
- 验证字符串中是否存在无效字符
- 如何检查字符串值是否存在于字符数组中
- 如何自动查找字符串中字符存在的次数
- 如何将数字拆分为其数字并将它们相加,如果存在任何字符,则应"invalid input"输出?
- 多字节到宽字符转换后,内存中存在临时副本
- 如何检查uint8_t是否作为类型而不是无符号字符存在
- 文件输出中存在错误字符
- 给定一个二维字符矩阵,我们必须检查给定的单词是否存在于其中
- c++11 regex:检查字符串中是否存在一组字符