C++ String capacity() FunctionLast Updated : 17 Mar 2025 In the C++ programming language, the capacity() function returns the total number of characters the string can store before requiring a reallocation. It is always greater than or equal to the string's current size() function. The capacity grows automatically as the string expands. ![]() In C++, this function gives the current size of the allocated space for the string. The capacity of the string is not necessarily equal to the size or length of the string. If the capacity is greater than the size, it means extra space is allocated for other string operations. Syntax:It has the following syntax: In this syntax,
Simple C++ String capacity() function ExampleLet us take an example to illustrate the string capacity() function in C++. ExampleCompile and RunOutput: String: Hello C++ Programming Language Size of the String: 30 Capacity of the String: 30 Explanation: In this example, we demonstrate how the string capacity() function reveals the memory allocated in the string. After that, it displays the string, its actual size, and the capacity. The capacity is usually larger than the size to allow efficient growth without frequent reallocations. Finally, the capacity value is implementation-dependent and can differ between systems or compilers. C++ String capacity() function Example After Adding New CharactersLet us take an example to illustrate the string capacity() function after including new characters in a C++ String. ExampleCompile and RunOutput: Size of the string: 42 Capacity of the string: 42 New Size of the String: 83 New Capacity of the String: 84 Explanation: In this example, we illustrate how the capacity() function shows the amount of memory allocated for a string. First, it shows the string's size and its current capacity. After adding more text to the string, the size increases, and the capacity also expands automatically to accommodate the new characters. Finally, it shows how C++ strings manage memory dynamically for efficient performance. C++ String Capacity() Function Example Using LoopsNow, we are taking a simple example to illustrate the string capacity() function using loops in C++. ExampleCompile and RunOutput: Size of the String: 1 Capacity of the String: 15 Size of the String: 2 Capacity of the String: 15 Size of the String: 3 Capacity of the String: 15 Size of the String: 4 Capacity of the String: 15 Size of the String: 5 Capacity of the String: 15 Explanation: In this example, we describe how a string's capacity grows automatically when we include the characters in the string. In every iteration, one character is included, and the program shows the updated size and capacity. After that, the capacity increases in larger steps to minimize frequent memory reallocations, which shows how C++ optimizes string storage. C++ String capacity() Function Example using the shrink_to_fit to Reduce CapacityNow, we are going to take an example to demonstrate the string capacity function using the shrink_to_fit function to reduce the capacity of the C++ string. ExampleCompile and RunOutput: The initial capacity of the string: 39 After reserve(55): The capacity of the string: 78 After using the shrink() function: The capacity of the string: 39 Explanation: In this example, we have created a string named "text" that is initialized with text. Firstly, it displays the default capacity of the string. By using the reserve(55) function, the capacity is increased to accommodate more characters without reallocation. Finally, the shrink_to_fit() function reduces any unused memory, which demonstrates how C++ optimizes string storage when requested. Features of the String capacity() function in C++There are several features of the string capacity() function in C++. Some of them are as follows:
ConclusionIn conclusion, the capacity() function in C++ provides insight into the memory allocated for a string, which shows how much it can grow before requiring reallocation. It helps to enhance performance by reducing frequent memory resizing at the time of string modifications. As the capacity increases automatically, we can handle it manually using the reserve() function or reduce unused memory using the shrink_to_fit() function. Overall, the capacity() function is an effective method to optimize string memory usage in C++. C++ String capacity() Function FAQs1) What is the string capacity() function in C++? In the C++ programming language, the string capacity() function returns the total number of characters the string can store before requiring a reallocation. It is always greater than or equal to the string's current size() function. 2) Does the capacity() function always equal the size of the string in C++? No, the capacity() function does not always equal the size of the string in C++. The capacity() function is greater than or equal to the size of the string. The C++ programming language allocates more memory for efficient growth in the program. 3) Can we decrease the capacity of the string in C++? No, we cannot decrease the capacity of the string in C++. It doesn't shrink automatically. However, when we call the shrink_to_fit() function, it requests a reduction of the capacity, but it is not binding. 4) Is capacity always greater than or equal to size? Yes, the capacity is always greater than or equal to size, because the string always contains huge memory storage to store all its characters. 5) Why does the capacity() function differ from other systems or compilers in C++? The capacity() function in C++ differs from other systems or compilers because the string memory allocation strategy is implementation-dependent. Therefore, different compilers can allocate different capacities in the C++ program. Next TopicC++ Strings |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India
