Horje
cpp string find all occurence Code Example
find all occurrences of a substring in a string c++
#include <string>
#include <iostream>

using namespace std;

int main()
{
    string s("hello hello");
    int count = 0;
    size_t nPos = s.find("hello", 0); // first occurrence
    while(nPos != string::npos)
    {
        count++;
        nPos = s.find("hello", nPos + 1);
    }

    cout << count;
};
cpp string find all occurence
string str,sub; // str is string to search, sub is the substring to search for

vector<size_t> positions; // holds all the positions that sub occurs within str

size_t pos = str.find(sub, 0);
while(pos != string::npos)
{
    positions.push_back(pos);
    pos = str.find(sub,pos+1);
}




Cpp

Related
c++ erase remove Code Example c++ erase remove Code Example
cudamemcpy Code Example cudamemcpy Code Example
c++ write to csv file append Code Example c++ write to csv file append Code Example
flutter run specific device Code Example flutter run specific device Code Example
c++ open all files in directory Code Example c++ open all files in directory Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
9