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.
- append() : lets you specify the appended value by using multiple arguments.
- push_back() : lets you to append single character at a time.
Here are few standards we can have for comparison among these three:
- += : We can append full string using +=.
- append() : We can also append full string using append().
- push_back : doesn’t allow appending of full string.
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.
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().
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.
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() : Doesn’t allow appending single character.
- push_back : Allows appending single character.
Original String : AB Using += : ABC Using push_back : ABC
- += : Doesn’t provide iterator range.
- append() : Provides iterator range.
- push_back : Doesn’t provide iterator range.
Original String : Hello World! Using append : Hello World! forGeeks
- += : Return *this.
- append() : Returns *this
- push_back : Doesn’t return anything.
Original String : Hello World! Using append : Hello World! GeeksforGeeks
This article is contributed by Sakshi Tiwari. If you like GeeksforGeeks(We know you do!) and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- vector::operator= and vector::operator[ ] in C++ STL
- deque::operator= and deque::operator in C++ STL
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Operator Overloading '<<' and '>>' operator in a linked list class
- Operands for sizeof operator
- Copy constructor vs assignment operator in C++
- Self assignment check in assignment operator
- Rules for operator overloading
- Result of comma operator as l-value in C and C++
- Comma operator should be used carefully
- C/C++ Ternary Operator - Some Interesting Observations
- Is assignment operator inherited?
- When should we write our own assignment operator in C++?
- Default Assignment Operator and References
- Can we use % operator on floating point numbers?
- Scope Resolution Operator Versus this pointer in C++?
- Set a variable without using Arithmetic, Relational or Conditional Operator
- new operator vs newInstance() method in Java
- Placement new operator in C++
- Operator Overloading in C++