std::string::append vs std::string::push_back() vs Operator += in C++
Last Updated :
08 Nov, 2022
To append characters, you can use operator +=, append(), and push_back(). All of them helps to append character but with a little difference in implementation and application.
- Operator += : appends single-argument values. Time complexity : O(n)
- append() : lets you specify the appended value by using multiple arguments. Time complexity: O(n)
- push_back() : lets you to append single character at a time. Time complexity: O(1)

Here are few standards we can have for comparison among these three:
1) Full String:
- += : We can append full string using +=.
- append() : We can also append full string using append().
- push_back : doesn't allow appending of full string.
Implementation:
CPP
// CPP code for comparison on the
// basis of appending Full String
#include <iostream>
#include <string>
using namespace std;
// Function to demonstrate comparison among
// +=, append(), push_back()
void appendDemo(string str1, string str2)
{
string str = str1;
// Appending using +=
str1 += str2;
cout << "Using += : ";
cout << str1 << endl;
// Appending using append()
str.append(str2);
cout << "Using append() : ";
cout << str << endl;
}
// Driver code
int main()
{
string str1("Hello World! ");
string str2("GeeksforGeeks");
cout << "Original String : " << str1 << endl;
appendDemo(str1, str2);
return 0;
}
OutputOriginal String : Hello World!
Using += : Hello World! GeeksforGeeks
Using append() : Hello World! GeeksforGeeks
2) Appending part of String:
- += : Doesn't allow appending part of string.
- append() : Allows appending part of string.
- push_back : We can't append part of string using push_back.
Implementation:
CPP
// CPP code for comparison on the basis of
// Appending part of string
#include <iostream>
#include <string>
using namespace std;
// Function to demonstrate comparison among
// +=, append(), push_back()
void appendDemo(string str1, string str2)
{
// Appends 5 characters from 0th index of
// str2 to str1
str1.append(str2, 0, 5);
cout << "Using append() : ";
cout << str1;
}
// Driver code
int main()
{
string str1("GeeksforGeeks ");
string str2("Hello World! ");
cout << "Original String : " << str1 << endl;
appendDemo(str1, str2);
return 0;
}
OutputOriginal String : GeeksforGeeks
Using append() : GeeksforGeeks Hello
3) Appending C-string (char*):
- += : Allows appending C-string
- append() : It also allows appending C-string
- push_back : We cannot append C-string using push_back().
Implementation:
CPP
// CPP code for comparison on the basis of
// Appending C-string
#include <iostream>
#include <string>
using namespace std;
// Function to demonstrate comparison among
// +=, append(), push_back()
void appendDemo(string str)
{
string str1 = str;
// Appending using +=
str += "GeeksforGeeks";
cout << "Using += : ";
cout << str << endl;
// Appending using append()
str1.append("GeeksforGeeks");
cout << "Using append() : ";
cout << str1 << endl;
}
// Driver code
int main()
{
string str("World of ");
cout << "Original String : " << str << endl;
appendDemo(str);
return 0;
}
OutputOriginal String : World of
Using += : World of GeeksforGeeks
Using append() : World of GeeksforGeeks
4) Appending character array:
- += : Allows appending of character array
- append() : Allows appending of character array.
- push_back : Does not allow char array appending.
Implementation:
CPP
// CPP code for comparison on the basis of
// Appending character array
#include <iostream>
#include <string>
using namespace std;
// Function to demonstrate comparison among
// +=, append(), push_back()
void appendDemo(string str)
{
char ch[6] = { 'G', 'e', 'e', 'k', 's', '\0' };
string str1 = str;
// Appending using +=
str += ch;
cout << "Using += : " << str << endl;
// Appending using append()
str1.append(ch);
cout << "Using append() : ";
cout << str1 << endl;
}
// Driver code
int main()
{
string str("World of ");
cout << "Original String : " << str << endl;
appendDemo(str);
return 0;
}
OutputOriginal String : World of
Using += : World of Geeks
Using append() : World of Geeks
5) Appending single character:
- += : We can append single character using += operator.
- append() : Allows appending single character.
- push_back : Allows appending single character.
CPP
// CPP code for comparison on the basis of
// Appending single character
#include <iostream>
#include <string>
using namespace std;
// Function to demonstrate comparison among
// +=, append(), push_back()
void appendDemo(string str)
{
string str1 = str;
string str2 = str;
// Appending using +=
str += 'C';
cout << "Using += : " << str << endl;
// Appending using append()
str2.append("C");
cout << "Using append() : ";
cout << str2 << endl;
// Appending using push_back()
str1.push_back('C');
cout << "Using push_back : ";
cout << str1;
}
// Driver code
int main()
{
string str("AB");
cout << "Original String : " << str << endl;
appendDemo(str);
return 0;
}
OutputOriginal String : AB
Using += : ABC
Using append() : ABC
Using push_back : ABC
6) Iterator range:
- += : Doesn't provide iterator range.
- append() : Provides iterator range.
- push_back : Doesn't provide iterator range.
Implementation:
CPP
// CPP code for comparison on the basis of
// Appending using iterator range
#include <iostream>
#include <string>
using namespace std;
// Function to demonstrate comparison among
// +=, append(), push_back()
void appendDemo(string str1, string str2)
{
// Appends all characters from
// str2.begin()+5, str2.end() to str1
str1.append(str2.begin() + 5, str2.end());
cout << "Using append : ";
cout << str1;
}
// Driver code
int main()
{
string str1("Hello World! ");
string str2("GeeksforGeeks");
cout << "Original String : " << str1 << endl;
appendDemo(str1, str2);
return 0;
}
OutputOriginal String : Hello World!
Using append : Hello World! forGeeks
7) Return Value:
- += : Return *this.
- append() : Returns *this
- push_back : Doesn't return anything.
Implementation:
CPP
// CPP code for comparison on the basis of
// Return value
#include <iostream>
#include <string>
using namespace std;
// Function to demonstrate comparison among
// +=, append(), push_back()
string appendDemo(string str1, string str2)
{
// Appends str2 in str1
str1.append(str2); // Similarly with str1 += str2
cout << "Using append : ";
// Returns *this
return str1;
}
// Driver code
int main()
{
string str1("Hello World! ");
string str2("GeeksforGeeks");
string str;
cout << "Original String : " << str1 << endl;
str = appendDemo(str1, str2);
cout << str;
return 0;
}
OutputOriginal String : Hello World!
Using append : Hello World! GeeksforGeeks
If you like GeeksforGeeks(We know you do!) and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected].
Similar Reads
std::string::push_back() in C++ The std::string::push_back() method in C++ is used to append a single character at the end of string. It is the member function of std::string class defined inside <string> header file. In this article, we will learn about std::string::push_back() method in C++.Example:C++// C++ Program to ill
2 min read
std::string::length, std::string::capacity, std::string::size in C++ STL Prerequisite: String in C++ String class is one of the features provided by the Standard template library to us, So it comes up with great functionality associated with it. With these Functionalities, we can perform many tasks easily. Let's see a few of the functionalities string class provides. Hea
6 min read
string::rbegin() and string::rend() in C++ The std::string::rbegin() and std::string::rend() functions in C++ are used to fetch the reverse iterators to the string. They are the member function of std::string and are used when we want to iterate the string in reverse. In this article, we will learn how to use string::rbegin() and string::ren
2 min read
char* vs std:string vs char[] in C++ In this article, we are going to inspect three different ways of initializing strings in C++ and discuss differences between them. 1. Using char* Here, str is basically a pointer to the (const)string literal. Syntax: char* str = "This is GeeksForGeeks";Pros: 1. Only one pointer is required to refer
6 min read
Difference between concatenation of strings using (str += s) and (str = str + s) A string is a collection of characters. For example, "GeeksforGeeks" is a string. C++ provides primitive data types to create a string. The string can also be initialized at the time of declaration. Syntax: string str; string str = "GeeksforGeeks" Here, "GeeksforGeeks" is a string literal. This arti
15+ min read