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
#include <iostream>
#include <string>
using namespace std;
void appendDemo(string str1, string str2)
{
string str = str1;
str1 += str2;
cout << "Using += : " ;
cout << str1 << endl;
str.append(str2);
cout << "Using append() : " ;
cout << str << endl;
}
int main()
{
string str1( "Hello World! " );
string str2( "GeeksforGeeks" );
cout << "Original String : " << str1 << endl;
appendDemo(str1, str2);
return 0;
}
|
Output
Original 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
#include <iostream>
#include <string>
using namespace std;
void appendDemo(string str1, string str2)
{
str1.append(str2, 0, 5);
cout << "Using append() : " ;
cout << str1;
}
int main()
{
string str1( "GeeksforGeeks " );
string str2( "Hello World! " );
cout << "Original String : " << str1 << endl;
appendDemo(str1, str2);
return 0;
}
|
Output
Original 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
#include <iostream>
#include <string>
using namespace std;
void appendDemo(string str)
{
string str1 = str;
str += "GeeksforGeeks" ;
cout << "Using += : " ;
cout << str << endl;
str1.append( "GeeksforGeeks" );
cout << "Using append() : " ;
cout << str1 << endl;
}
int main()
{
string str( "World of " );
cout << "Original String : " << str << endl;
appendDemo(str);
return 0;
}
|
Output
Original 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
#include <iostream>
#include <string>
using namespace std;
void appendDemo(string str)
{
char ch[6] = { 'G' , 'e' , 'e' , 'k' , 's' , '\0' };
string str1 = str;
str += ch;
cout << "Using += : " << str << endl;
str1.append(ch);
cout << "Using append() : " ;
cout << str1 << endl;
}
int main()
{
string str( "World of " );
cout << "Original String : " << str << endl;
appendDemo(str);
return 0;
}
|
Output
Original 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
#include <iostream>
#include <string>
using namespace std;
void appendDemo(string str)
{
string str1 = str;
string str2 = str;
str += 'C' ;
cout << "Using += : " << str << endl;
str2.append( "C" );
cout << "Using append() : " ;
cout << str2 << endl;
str1.push_back( 'C' );
cout << "Using push_back : " ;
cout << str1;
}
int main()
{
string str( "AB" );
cout << "Original String : " << str << endl;
appendDemo(str);
return 0;
}
|
Output
Original 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
#include <iostream>
#include <string>
using namespace std;
void appendDemo(string str1, string str2)
{
str1.append(str2.begin() + 5, str2.end());
cout << "Using append : " ;
cout << str1;
}
int main()
{
string str1( "Hello World! " );
string str2( "GeeksforGeeks" );
cout << "Original String : " << str1 << endl;
appendDemo(str1, str2);
return 0;
}
|
Output
Original String : Hello World!
Using append : Hello World! forGeeks
7) Return Value:
- += : Return *this.
- append() : Returns *this
- push_back : Doesn’t return anything.
Implementation:
CPP
#include <iostream>
#include <string>
using namespace std;
string appendDemo(string str1, string str2)
{
str1.append(str2);
cout << "Using append : " ;
return str1;
}
int main()
{
string str1( "Hello World! " );
string str2( "GeeksforGeeks" );
string str;
cout << "Original String : " << str1 << endl;
str = appendDemo(str1, str2);
cout << str;
return 0;
}
|
Output
Original 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 review-team@geeksforgeeks.org.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...