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:
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.
#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.
#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:
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.
#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: