C++ 字符串.查找()

C++ string.find(' ')

本文关键字:查找 字符串 C++      更新时间:2023-10-16

目标是使用 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,但是我需要找到每个单个"answers" )的位置。请帮助,

预先感谢

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