Use STL find() algorithm to obtain an iterator to the start of the first 'a' - C++ STL

C++ examples for STL:string

Description

Use STL find() algorithm to obtain an iterator to the start of the first 'a'

Demo Code

#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <vector>
using namespace std;
int main()/* w w  w .  ja  v  a  2 s  . c o  m*/
{
   string strA("This is a test. another test test test");
   // Create an iterator to a string.
   string::iterator itr;
   // Insert into a string via an iterator.
   // use find() algorithm to obtain an iterator to the start of the first 'a'.
   itr = find(strA.begin(), strA.end(), 'a');
   // increment the iterator so that it points to the character after 'a', which in this case is a space.
   ++itr;
   // Insert into str by using the iterator version of insert().

   cout <<"Insert into a string via an iterator.\n";

   string strB(" bigger");

   strA.insert(itr, strB.begin(), strB.end());

   cout << strA << "\n\n";

   return 0;
}

Result


Related Tutorials