Use push_back() to add characters to a string. - C++ STL

C++ examples for STL:string

Description

Use push_back() to add characters to a string.

Demo Code

#include <iostream>
#include <string>
using namespace std;
int main()/*ww  w  .j  ava 2 s  .  com*/
{
   string str1("Alpha");
   string str2("Beta");
   string str3("Gamma");
   string str4;
   str4 = str1 + str3;
   for(char ch = 'A'; ch <= 'Z'; ++ch)
      str4.push_back(ch);
   cout << "str4 after calls to push_back(): " << str4 << endl;
   cout << "Size and capacity of str4 is now " << str4.size() << " " << str4.capacity() << "\n\n";
   return 0;
}

Result


Related Tutorials