Remove all substrings from a string with template - C++ STL

C++ examples for STL:string

Description

Remove all substrings from a string with template

Demo Code

#include <string>
#include <iostream>

using namespace std;

template<typename T>
void removeSubstrs(basic_string<T>& s, const basic_string<T>& p) {
   basic_string<T>::size_type n = p.length();

   for (basic_string<T>::size_type i = s.find(p);i != basic_string<T>::npos;i = s.find(p))
      s.erase(i, n);//from w  w w.ja  v  a 2s.  co m
}
int main() {
   string s = "this is a test test one test two test";
   string p = "test";

   removeSubstrs(s, p);

   cout << s << '\n';
}

Result


Related Tutorials