目标是使用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; 的位置。请帮忙,
提前致谢
答案 0 :(得分:1)
find
的第二个(显然是可选的)参数指定字符串中开始搜索的位置;用它来跳过你已经找到的事件。