VOOZH about

URL: https://www.geeksforgeeks.org/cpp/stdstringpush_back-in-cpp/

⇱ std::string::push_back() in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

std::string::push_back() in C++

Last Updated : 23 Jul, 2025

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:


Output
Geeks

string::push_back() Syntax

s.push_back(c);

where s the name of the string.

Parameters

  • c: Character to append.

Return Value

  • This 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 Analysis

The 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 Constant
Auxiliary 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()


Output
Hello

Example 2: Creating a Pattern using string::push_back() in Loop


Output
*-*-*-*-*-*
Comment
Article Tags: