记录7
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
int cnt=0;
cin>>s;
for(int i=0;i<s.size();i++){
if(s[i]=='1') cnt++;
}
cout<<cnt;
return 0;
}
突破点
字符串中究竟有多少个 1
思路
统计字符串中1的个数,依次遍历并计数
代码简析
for(int i=0;i<s.size();i++){
if(s[i]=='1') cnt++;
}
string类型的字符串,其实就是一个数组,可以用下标遍历
不知道字符串s的长度,可以用size()函数
注意:
size()和length():
这两个函数是
string类的成员函数,用于获取string对象的长度。它们的功能完全相同,返回值类型是
std::string::size_type,通常是size_t。
strlen():
这是C语言标准库函数,用于获取C风格字符串(以空字符结束的字符数组)的长度。
它返回的是
size_t类型的值,表示字符串的长度,不包括结尾的空字符。使用场景
如果你使用的是
string对象,可以使用size()或length()。如果你处理的是C风格字符串(字符数组),应该使用
strlen()。
再注意:
sizeof():
sizeof是C++中的一个运算符,用于获取变量或类型所占用的字节数。sizeof的结果类型是size_t,通常用于动态内存分配和数组操作中。
sizeof计算的是变量或类型所占用的字节数。对于C风格字符串(字符数组),
sizeof会计算整个数组的大小,包括结尾的空字符\0。对于
string对象,sizeof计算的是对象本身的大小,而不是字符串内容的大小。
补充
处理字符串时经常需要统计字符的出现次数时,一些常用的的手段
在C++中,处理字符串时经常需要统计字符的出现次数。以下是一些常用的方法,条理清晰,分级明确,帮助你更好地理解和应用这些方法。
1. 使用
std::map统计字符出现次数
std::map是一个关联容器,可以方便地统计字符的出现次数。1.1 方法1:使用
std::map
步骤:
遍历字符串中的每个字符。
使用
std::map统计每个字符的出现次数。代码示例:
#include <iostream> #include <string> #include <map> using namespace std; int main() { string str = "hello world"; map<char, int> charCount; for (char c : str) { charCount[c]++; } for (const auto& pair : charCount) { cout << pair.first << ": " << pair.second << endl; } return 0; }1.2 输出结果
: 1 d: 1 e: 1 h: 1 l: 3 o: 2 r: 1 w: 12. 使用
std::unordered_map统计字符出现次数
std::unordered_map是一个哈希表,通常比std::map更高效,特别是在处理大量数据时。2.1 方法2:使用
std::unordered_map
步骤:
遍历字符串中的每个字符。
使用
std::unordered_map统计每个字符的出现次数。代码示例:
#include <iostream> #include <string> #include <unordered_map> using namespace std; int main() { string str = "hello world"; unordered_map<char, int> charCount; for (char c : str) { charCount[c]++; } for (const auto& pair : charCount) { cout << pair.first << ": " << pair.second << endl; } return 0; }2.2 输出结果
: 1 d: 1 e: 1 h: 1 l: 3 o: 2 r: 1 w: 13. 使用数组统计字符出现次数
如果字符范围已知(如ASCII字符),可以使用数组来统计字符的出现次数。这种方法通常比
std::map和std::unordered_map更高效。3.1 方法3:使用数组
步骤:
初始化一个大小为256的数组(假设字符范围为ASCII)。
遍历字符串中的每个字符,将字符的ASCII值作为数组索引,统计出现次数。
代码示例:
#include <iostream> #include <string> #include <vector> using namespace std; int main() { string str = "hello world"; vector<int> charCount(256, 0); // ASCII字符范围 for (char c : str) { charCount[static_cast<unsigned char>(c)]++; } for (int i = 0; i < 256; i++) { if (charCount[i] > 0) { cout << static_cast<char>(i) << ": " << charCount[i] << endl; } } return 0; }3.2 输出结果
: 1 d: 1 e: 1 h: 1 l: 3 o: 2 r: 1 w: 14. 统计特定字符的出现次数
如果只需要统计某些特定字符的出现次数,可以使用
std::count函数。4.1 方法4:使用
std::count
步骤:
使用
std::count函数统计特定字符的出现次数。代码示例:
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string str = "hello world"; char target = 'l'; int count = count(str.begin(), str.end(), target); cout << "Character '" << target << "' appears " << count << " times." << endl; return 0; }4.2 输出结果
Character 'l' appears 3 times.5. 统计字符的频率并排序
如果需要统计字符的频率并按频率排序,可以结合
std::map和std::vector来实现。5.1 方法5:统计频率并排序
步骤:
使用
std::map统计字符的出现次数。将结果存储到
std::vector中。按频率排序。
代码示例:
#include <iostream> #include <string> #include <map> #include <vector> #include <algorithm> using namespace std; int main() { string str = "hello world"; map<char, int> charCount; for (char c : str) { charCount[c]++; } vector<pair<char, int>> freqVec(charCount.begin(), charCount.end()); sort(freqVec.begin(), freqVec.end(), [](const pair<char, int>& a, const pair<char, int>& b) { return a.second > b.second; }); for (const auto& pair : freqVec) { cout << pair.first << ": " << pair.second << endl; } return 0; }5.2 输出结果
l: 3 o: 2 : 1 d: 1 e: 1 h: 1 r: 1 w: 1总结
使用
std::map:适合统计字符出现次数,但效率较低。使用
std::unordered_map:适合统计字符出现次数,效率较高。使用数组:适合统计ASCII字符出现次数,效率最高。
使用
std::count:适合统计特定字符的出现次数。统计频率并排序:结合
std::map和std::vector,可以统计字符频率并按频率排序。这些方法在处理字符串时非常有用。
1421

被折叠的 条评论
为什么被折叠?



