c++ - 尽管我的函数已被调用,但编译器不输出任何内容
问题描述
我正在尝试创建一个程序来计算一个字母在字符串中出现的次数,但是下面的程序没有输出任何内容,尽管该函数count()
确实被调用了。
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int count(const string &s, char c) {
string::const_iterator i = find(s.begin(), s.end(), c);
int n = 0;
while (i != s.end()) {
++n;
i = find(i+1, s.end(), c);
}
return n;
}
int main() {
const string e = "dddddddd";
char d = 'd';
count(e, d);
}
解决方案
您应该使用标准输出函数 cout 来输出结果。
std::cout << count(e,d);
Return 不输出任何内容。
推荐阅读
- pattern-matching - GADT 上的模式匹配失败
- r - 为什么以下函数在给定非空输入的情况下返回 NULL 以及何时应该返回已处理输入的向量?
- ckeditor - 将对话框数据传递给 CKEditor 小部件模板
- for-loop - For 循环删除 Google Apps 脚本中可能存在的多列
- excel - 选择 Excel 单元格时在 VBA 中选择范围类的方法失败
- react-native - 如何使用 react-native-google-places-autocomplete
- python - 将字符串作为参数传递时出错(Solidity)
- apache-spark - 我的机器上的 Pyspark 比我同事的慢,即使我们有同一台机器
- c# - 基于服务器位置的 DistributedCache 到期时间
- angular - 为什么我的服务人员请求一个不安全的页面?