C++ 使用 find(str, index) 计算字符串中某个字符的出现次数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8870263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:15:45  来源:igfitidea点击:

C++ To count the occurrences of a char in a string using find(str, index)

c++stringcountfind

提问by delphi316

This is actually my homework and the question states:

这实际上是我的作业,问题指出:

"The program should determine how many times the character is contained in the string. (Hint: Search the string by using the find(str,?ind) method. This method should be used in a loop that starts the index value at 0 and then changes the index value to 1 past the index of where the char was last found.)"

“程序应该确定该字符在字符串中包含多少次。(提示:使用 find(str,?ind) 方法搜索字符串。该方法应该在索引值从 0 开始的循环中使用,并且然后将索引值更改为 1 超过上次找到字符的位置的索引。)”

This is what I've came up with but all it does is count how many character there is in the string. New to C++ so I hope you guys can be patient with me.

这是我想出的,但它所做的只是计算字符串中有多少个字符。刚接触 C++,所以我希望你们能对我有耐心。

#include <string>
#include <iostream>
using namespace std;

int main()
{
    string s;
    char c;
    size_t contain;
    int count = 0;
    cout << "Enter a string : ";
    getline(cin, s);
    cout <<"Enter a char : ";
    cin >> c;
    for(int i = 0; i < s.length(); i++)
    {
        contain = s.find(c, i);
        if (contain =! string::npos )
        {
            count++;
        }
    }
    cout << count <<endl;
    return 0;
}

回答by Oliver Charlesworth

This:

这个:

(contain =! string::npos)
         ^^

doesn't do what you think it does. Take a look at http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B.

不会做你认为它会做的事情。看看http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

Also, this:

还有这个:

contain = s.find(c, i);

doesn't do anything particularly useful (if all you're doing is incrementing ievery iteration). You'll end up counting some occurrences multiple times.

没有做任何特别有用的事情(如果你所做的只是增加i每次迭代)。您最终会多次计算某些出现次数。

[Note:You can solve the actual task much more cleanly by using count = std::count(s.begin(), s.end(), c).]

[注意:您可以使用count = std::count(s.begin(), s.end(), c). ]

回答by ddacot

You can do simply :

你可以简单地做:



Instead of

代替

contain = s.find(c, i);
if (contain =! string::npos )
    {
        count++;
    }

Write

if(s[i] == c) 
{
   count++;
}


also, you can use this.

此外,您可以使用它。

#include <algorithm>
int count = std::count(s.begin(), s.end(), c);
cout<<count;

回答by Martin Kristiansen

I think @parapura's code looks nice'er like this:

我认为@parapura 的代码看起来更像这样:

while((size_t contain = s.find(c,i)) != string::npos){
    count++;
    i = contain + 1;
}

and it solves the problem nicely ;-)

它很好地解决了这个问题;-)

回答by parapura rajkumar

I think according to your problem statement.

我认为根据你的问题陈述。

changes the index value to 1 past the index of where the char was last found

将索引值更改为 1 超过上次找到字符的位置的索引

Your while loop should be something like

你的while循环应该是这样的

while( 1 )
{
    contain = s.find(c, i);
    if (contain != string::npos )
    {
        count++;
        i = contain + 1;
    }
    else
    {
        break;
    }
}