std::string::push_back() in C++ Last Updated : 22 Oct, 2024 Comments Improve Suggest changes Like Article Like Report 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 illustrate how to use // std::string::push_back() method #include <bits/stdc++.h> using namespace std; int main() { string s = "Geek"; // Appending character to the string s.push_back('s'); cout << s; return 0; } OutputGeeksstring::push_back() Syntaxs.push_back(c);where s the name of the string.Parametersc: Character to append.Return ValueThis function does not return any value.Note: This function only works for C++ Style strings i.e. instances of std::string class and doesn't work for C-Style strings i.e. array of characters.Complexity AnalysisThe std::string container is generally implemented using dynamic arrays. So, std::string::push_back() function basically adds a character to the end of the internal dynamic array.Now, with dynamic arrays, if they have enough space, then the insertion at the end is fast O(1), but if they don't have enough space, then a reallocation might be needed, and it may take O(n) time. But the algorithm used for reallocation adjust the overall complexity to bring it to the amortized constant.Time Complexity: O(1), Amortized ConstantAuxiliary Space: O(1)More Examples of string::push_back()The following examples illustrate the use of string::push_back() method is different cases.Example 1: Constructing Whole String using string::push_back() C++ // C++ program to construct whole string using // string::push_back() method #include <bits/stdc++.h> using namespace std; int main() { string s; // Constructing string character by character s.push_back('H'); s.push_back('e'); s.push_back('l'); s.push_back('l'); s.push_back('o'); cout << s; return 0; } OutputHelloExample 2: Creating a Pattern using string::push_back() in Loop C++ #include <iostream> #include <string> using namespace std; int main() { string s = "*"; // Append '-' and '*' alternatively for (int i = 0; i < 5; ++i) { s.push_back('-'); s.push_back('*'); } cout << s << endl; return 0; } Output*-*-*-*-*-* Comment More infoAdvertise with us Next Article std::string::push_back() in C++ S Sakshi Tiwari Improve Article Tags : C++ STL cpp-string cpp-strings-library Practice Tags : CPPSTL Similar Reads Vector push_back() in C++ STL In C++, the vector push_back() is a built-in method used to add a new element at the end of the vector. It automatically resizes the vector if there is not enough space to accommodate the new element.Letâs take a look at an example that illustrates the vector push_back() method:C++#include <bits/ 2 min read std::basic_string::at in C++ Returns a reference to the character at the specified location pos. The function automatically checks whether pos is the valid position of a character in the string (i.e., whether pos is less than the string length), throwing an out_of_range exception if it is not. Syntax: reference at (size_type po 1 min read std::basic_string::operator[] in C++ Returns a reference to the character at specified location pos. No bounds checking is performed. If pos > size(), the behavior is undefined. Syntax : reference operator[] (size_type pos); const_reference operator[] (size_type pos) const; Parameters : pos - position of the character to return Retu 1 min read std::string::size() in C++ The std::string::size() function in C++ is a built-in method of the std::string class that is used to determine the number of characters in the string. All characters up to the null character are considered while calculating the size of the string.In this article, we will learn about string::size() 2 min read std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. The string class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.String vs Character ArrayStringChar 8 min read std::string::clear in C++ The string content is set to an empty string, erasing any previous content and thus leaving its size at 0 characters. Parameters: none Return Value: none void string ::clear () - Removes all characters (makes string empty) - Doesn't throw any error - Receives no parameters and returns nothing CPP // 1 min read std::string::assign() in C++ The member function assign() is used for the assignments, it assigns a new value to the string, replacing its current contents. Syntax 1: Assign the value of string str. string& string::assign (const string& str) str : is the string to be assigned. Returns : *this CPP // CPP code for assign 5 min read std::string::append() in C++ The string::append() in C++ is used append the characters or the string at the end of the string. It is the member function of std::string class .The string::append() function can be used to do the following append operations:Table of ContentAppend a Whole StringAppend a Part of the StringAppend a C 3 min read std::string::insert() in C++ In C++, the string insert() function is used to insert characters or a string at the given position of the string. For example,C++#include <bits/stdc++.h> using namespace std; int main() { string s = "Geeks"; // Inserting another string at the friend // of s s.insert(s.size(), "forGeeks"); cou 4 min read std::string::resize() in C++ resize() lets you change the number of characters. Here we will describe two syntaxes supported by std::string::resize() in C++ Return Value : None Syntax 1: Resize the number of characters of *this to num. void string ::resize (size_type num) num: New string length, expressed in number of character 3 min read Like