如何计算字符串中用户输入元素的出现次数?
How can I count the number of occurrences of a user inputed element in a string?
如何计算字符串中用户输入元素的出现次数? 例:
输入:ADEDDSDF输入:一个输出:1
你可以自己计算或使用stl
#include <algorithm>
#include <string>
int main()
{
constexpr char chFind = 'a';
std::string str = "abcabca";
// first solution
size_t num1 = 0;
for(size_t i = 0; i < str.size(); i++)
if (str[i] == chFind)
num1++;
// second solution
size_t num2 = std::count(str.begin(), str.end(), chFind);
return 0;
}
相关文章:
- C++如何通过用户输入删除列表元素
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 静态堆栈函数不会 1) 输入第一个元素 2)添加新元素时识别旧元素
- 如何输入数组的元素,每次获得新元素时,我们将其放在数组的中间?
- 将包含多个元素的字符串作为输入并转换为矢量 C++
- 如何计算字符串中用户输入元素的出现次数?
- 在数组中显示第一个元素用户输入
- Qt,操作缓慢时不要冻结GUI输入元素
- 函数返回包含输入容器的特定元素的容器
- 如何让用户C++输入少于 10 个元素的数组
- 如何将输入到具有未知数量元素的数组中?
- 与 C++ 中一行中的用户输入数组元素相关的问题
- 数组元素的用户输入
- C 中的动态数组(用指针表示) - 输入元素
- C 中结构的输入元素
- 如何使用用户输入正确修改 2D 数组中的字符数组元素?专门用于电影院座位
- 根据另一个数组的元素输入,而无需复制
- 如何更新最近在 std::vector<pair<int, int>> 中输入的元素?
- 用户将元素输入到 2D 矢量 c++ 中
- 将未知数据类型的元素输入到向量中并传递给函数模板