C++ string.find('')

问题描述:

目标是使用sentence.find('and')查找字符串中所有“ands”的位置。也必须使用int变量。C++ string.find('')

什么我迄今所做的:

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

} 

只计算18对所有3,但我需要找到每一个人“和”的位置。请帮帮忙,

在此先感谢

+0

使用文档。 http://www.cplusplus.com/reference/string/string/find/ –

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