c++ - Using std::search to find multiple occurrence of pattern -
i want use function search or other similar function find multiple occurrence of given pattern.
this code:
#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 = "abcabcabc"; string b = "abc"; string::iterator it; = search(a.begin(),a.end(),b.begin(),b.end()); if(it!=a.end()){ cout << it-a.begin()<<endl; } return 0; }
this code return 0 first occurrence of pattern "abc" , return 0, 3, 6. of indexes in original string pattern begins.
thank help.
for(size_t pos=a.find(b,0); pos!=std::string::npos; pos=a.find(b,pos+1)) { std::cout << pos << std::endl; }
this uses std::basic_string::find
(ref) directly find starting position of substring.
Comments
Post a Comment