C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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 : s1.push_back(ch); Parameters
ch : New character which is to be added. Return valueIt 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 2Let'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 3Let's see the example of inserting an element at the end of 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: Java
Next TopicC++ Strings
|