我想使用函數搜索或其他類似的函數來查找給定模式的多次出現。使用std :: search來查找多個出現的模式
這是我的代碼:
#include <cstring>
#include <iostream>
#include <iomanip>
#include <set>
#include <list>
#include <vector>
#include <map>
#include <algorithm>
#include <functional>
using namespace std;
int main() {
std::vector<int> haystack;
string a = "abcabcabc";
string b = "abc";
string::iterator it;
it = search(a.begin(),a.end(),b.begin(),b.end());
if(it!=a.end()){
cout << it-a.begin()<<endl;
}
return 0;
}
此代碼模式「ABC」的第一次出現返回0,想返回0,3,6,這將是所有指標的原模式開始的字符串。
謝謝你的幫助。
的[找到所有的子串的事件和地點]可能重複(http://stackoverflow.com/questions/4034750/find-all-a-substrings-occurrences-and-locations ) – CoryKramer
你需要一個循環,它應該在'it == a.end()'時完成。你嘗試了什麼?有什麼特別的原因,你沒有使用'a.find'? – Useless