简体   繁体   English

C++ string.find(' ')

[英]C++ string.find(' ')

The goal is to use the sentence.find('and') to find all of the positions of "ands" in the string.目标是使用sentence.find('and')查找字符串中“ands”的所有位置。 The int variable has to be used as well.还必须使用int变量。

What I've done so far:到目前为止我所做的:

#include <iostream>
#include <string>

using namespace std;
int main() {

    string sentence;
    int first, second, third;
    sentence = "My name is Ryan and I like sports and music and movies";
    first = sentence.find('and');
    second = sentence.find('and');
    third = sentence.find('and');

    cout << "Position of first and is: " << first << endl;
    cout << "Position of second and is: " << second << endl;
    cout << "Position of third and is: " << third << endl;

    system("pause");
    return 0;

}

It only computes as 18 for all 3 ,but I need to find the position of each individual "and" .它只对所有 3 个计算为 18 ,但我需要找到每个单独的“和”的位置。 Please help,请帮忙,

Thanks in advance提前致谢

The second (obviously optional) argument to find specifies where in the string to start searching from; find的第二个(显然是可选的)参数指定从字符串中的哪个位置开始搜索; use that to skip over the occurrences you have already found.使用它跳过您已经找到的事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM