P5660 [CSP-J2019] 数字游戏

记录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
  • 步骤

    1. 遍历字符串中的每个字符。

    2. 使用 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: 1

2. 使用 std::unordered_map 统计字符出现次数

std::unordered_map 是一个哈希表,通常比 std::map 更高效,特别是在处理大量数据时。

2.1 方法2:使用 std::unordered_map
  • 步骤

    1. 遍历字符串中的每个字符。

    2. 使用 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: 1

3. 使用数组统计字符出现次数

如果字符范围已知(如ASCII字符),可以使用数组来统计字符的出现次数。这种方法通常比 std::mapstd::unordered_map 更高效。

3.1 方法3:使用数组
  • 步骤

    1. 初始化一个大小为256的数组(假设字符范围为ASCII)。

    2. 遍历字符串中的每个字符,将字符的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: 1

4. 统计特定字符的出现次数

如果只需要统计某些特定字符的出现次数,可以使用 std::count 函数。

4.1 方法4:使用 std::count
  • 步骤

    1. 使用 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::mapstd::vector 来实现。

5.1 方法5:统计频率并排序
  • 步骤

    1. 使用 std::map 统计字符的出现次数。

    2. 将结果存储到 std::vector 中。

    3. 按频率排序。

  • 代码示例

#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::mapstd::vector,可以统计字符频率并按频率排序。

这些方法在处理字符串时非常有用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值