std::string::push_back() in C++
The push_back() member function is provided to append characters. Appends character c to the end of the string, increasing its length by one.
Syntax :
void string:: push_back (char c) Parameters: Character which to be appended. Return value: None Error: throws length_error if the resulting size exceeds the maximum number of characters(max_size).
// CPP code for to illustrate // std::string::push_back() #include <iostream> #include <string> using namespace std; // Function to demonstrate push_back() void push_backDemo(string str1, string str2) { // Appends character by character str2 // at the end of str1 for(int i = 0; str2[i] != '\0'; i++) { str1.push_back(str2[i]); } cout << "After push_back : "; cout << str1; } // Driver code int main() { string str1("Geeksfor"); string str2("Geeks"); cout << "Original String : " << str1 << endl; push_backDemo(str1, str2); return 0; } |
chevron_right
filter_none
Output:
Original String : Geeksfor After push_back : 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 contribute@geeksforgeeks.org. 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.
Recommended Posts:
- C++ | asm declaration
- Pointers and References in C++
- Strings in C++ and How to Create them?
- Enum Classes in C++ and Their Advantage over Enum DataType
- Deque vs Vector in C++ STL
- C++ Programming Basics
- Introduction to C++ Programming Language
- ios manipulators noshowpos() function in C++
- ios rdstate() function in C++ with Examples
- ios operator !() function in C++ with Examples
- ios operator() function in C++ with Examples
- ios clear() function in C++ with Examples
- ios good() function in C++ with Examples
- ios setstate() function in C++ with Examples
- ios eof() function in C++ with Examples



