String Push Back Function - C++ Programming Tutorial
C++ Course / Strings / String Push Back Function

String Push Back Function

BLUF: Mastering String Push Back Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: String Push Back Function

C++ is renowned for its efficiency. Learn how String Push Back Function enables low-level control and high-performance computing in the tutorial below.

C++ String push_back

This function is employed to append a new character, represented by the variable ch, at the conclusion of the string, thereby extending its length by one.

Syntax

Consider a string s1 and a character ch. The syntax for this operation is as follows:

Example

s1.push_back(ch);

Parameters

ch : New character which is to be added.

Return value

It does not return any value.

Example 1

Let's see simple example.

Example

#include<iostream>
using namespace std;
            int main()
           {
           string s1 = "Hell";
           cout<< "String is :" <<s1<<'\n';
           s1.push_back('o');
           cout<<"Now, string is :"<<s1;
          return 0;   
           }

Example 2

Let's consider another simple example.

Example

#include<iostream>
using namespace std;
int main()
{
string str = "java tutorial ";
cout<<"String contains :" <<str<<'\n';
str.push_back('1');
cout<<"Now,string is : "<<str;
return 0; 
}

Output:

Output

String contains :java tutorial
Now,string is java tutorial 1

Example 3

Let's explore a demonstration of appending an item to the conclusion of a vector.

Example

#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<char> s;
s.push_back('j');
s.push_back('a');
s.push_back('v');
s.push_back('a');
for(int i=0;i<s.size();i++)
cout<<s[i];
return 0;
}

Output:

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience