怎么在字符串(string)中查找出所有子串在母串中出现的位置。

怎么在字符串(string)中查找所有子串在母串中出现的位置。

img

谢谢了

给你做出来了,麻烦采纳一下 ,谢谢!

img

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
int main() {

    string s("1a2b3c4d5e6f7jkg8h9i1a2b3c4d5e6f7g8ha9i");
    // string flag;
 
    // find 函数 返回jk 在s 中的下标位置

    string flag = "a";
    int position = 0;
    int i = 1;
    while ((position = s.find(flag, position)) != string::npos) {
        cout << "position  " << i << " : " << position << endl;
        position++;
        i++;
    }
}