C ++ string.find('')

时间:2017-09-28 01:06:12

标签: c++ string find

目标是使用sentence.find('and')查找字符串中" ands" 的所有位置。还必须使用int变量。

  

到目前为止我做了什么:

#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;

}

它只计算所有3 的18,但我需要找到每个人&#34;和&#34; 的位置。请帮忙,

提前致谢

1 个答案:

答案 0 :(得分:1)

find的第二个(显然是可选的)参数指定字符串中开始搜索的位置;用它来跳过你已经找到的事件。