首页 > 解决方案 > 尽管我的函数已被调用,但编译器不输出任何内容

问题描述

我正在尝试创建一个程序来计算一个字母在字符串中出现的次数,但是下面的程序没有输出任何内容,尽管该函数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);
}

标签: c++iterator

解决方案


您应该使用标准输出函数 cout 来输出结果。

std::cout << count(e,d);

Return 不输出任何内容。


推荐阅读