C++ String push_back
This function is used to add new character ch at the end of the string, increasing its length by one.
Syntax
Consider a string s1 and character ch . Syntax would be :
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 see the example of inserting an element at the end of 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: