簡體   English   中英

C++ string.find(' ')

[英]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 ,但我需要找到每個單獨的“和”的位置。 請幫忙,

提前致謝

find的第二個(顯然是可選的)參數指定從字符串中的哪個位置開始搜索; 使用它跳過您已經找到的事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM