Insert into string by using the iterator version of insert() - C++ STL

C++ examples for STL:string

Description

Insert into string by using the iterator version of insert()

Demo Code

#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <vector>
using namespace std;
int main()//from w  ww. j  a  va  2s.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');

   // Next, 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