In C++, the capacity method provides the count of characters the string can accommodate before needing reallocation. This value is consistently higher than or equal to the current size of the string. The capacity dynamically increases as the string grows in size.
In C++, this function returns the current allocated memory size for the string. The capacity of the string may differ from its actual size or length. When the capacity exceeds the size, it indicates that additional space is reserved for other string manipulations.
Syntax:
It has the following syntax:
str.capacity();
In this particular syntax,
- Argument: This function does not include any arguments.
- Output: It provides the size of the string's presently allocated space.
Simple C++ String capacity function Example
Let's consider an example to demonstrate the functionality of the capacity method in C++.
Example
#include <iostream>
#include <string>
using namespace std; //using standard namespace
int main() { //main function
string st = "Hello C++ Programming Language";
cout <<"String: " << st << endl;
cout <<"Size of the String: " << st.size() << endl;
cout <<"Capacity of the String: " << st.capacity() << endl;
return 0;
}
Output:
String: Hello C++ Programming Language
Size of the String: 30
Capacity of the String: 30
Explanation:
In this illustration, we showcase how the capacity method of a string object exposes the amount of memory reserved for the string. Following this, it exhibits the string itself, its current size, and the allocated capacity. Typically, the capacity is greater than the size to enable effective expansion without constant memory reallocations. Ultimately, the specific capacity value is reliant on the implementation and may vary across different systems or compilers.
C++ String capacity function Example After Adding New Characters
Let's consider an example to demonstrate the string capacity method following the addition of new characters to a C++ String.
Example
#include <iostream>
#include <string>
using namespace std; //using standard namespace
int main() { //main function
string st = "Hello C++ Programming Language";
cout <<"String: " << st << endl;
cout <<"Size of the String: " << st.size() << endl;
cout <<"Capacity of the String: " << st.capacity() << endl;
return 0;
}
Output:
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 instance, we demonstrate how the capacity method reveals the memory allocated for a string. Initially, it displays the size of the string along with its existing capacity. Upon appending additional text to the string, both the size and capacity grow automatically to fit the additional characters. Ultimately, it highlights the dynamic memory management of C++ strings, ensuring optimal performance.
C++ String Capacity Function Example Using Loops
Now, we will demonstrate the functionality of the capacity method by utilizing loops in the C++ programming language.
Example
#include <iostream>
#include <string>
using namespace std; //using standard namespace
int main() { //main function
string tex;
for (int i = 0; i < 5; i++) {
tex += 'a';
cout <<"Size of the String: " << tex.size()
<<"\nCapacity of the String: " << tex.capacity() << endl;
}
return 0;
}
Output:
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 illustration, we explain the automatic expansion of a string's capacity as characters are added to it. With each iteration, a single character is appended, and the application displays the adjusted size and capacity. Subsequently, the capacity expands in larger increments to reduce the frequency of memory reallocations, demonstrating the efficiency of string storage optimization in C++.
C++ String capacity Function Example using the shrink_to_fit to Reduce Capacity
Now, let's illustrate the string capacity function with an example by utilizing the shrinktofit function to decrease the capacity of the C++ string.
Example
#include <iostream>
#include <string> //using string header file
using namespace std; //using standard namespace
int main() { //main function
string text = "Welcome to the C++ programming language";
cout << "The initial capacity of the string: " << text.capacity() << endl;
text.reserve(55); // increase capacity
cout << "After reserve(55): The capacity of the string: " << text.capacity() << endl;
text.shrink_to_fit(); // reduce unused memory
cout << "After using the shrink() function: The capacity of the string: " << text.capacity() << endl;
return 0;
}
Output:
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 instance, we've established a string titled "text" that's set with initial text values. Initially, it reveals the default size of the string. Through employing the reserve(55) method, the size expands to allow for additional characters without needing to relocate memory. Ultimately, the shrinktofit method trims any excess memory, showcasing how C++ enhances string storage efficiency upon demand.
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:
- The string capacity function in C++ returns the total number of characters the string may store before requiring reallocated memory.
- It can help to increase capacity to a specific value for performance optimization.
- When we need to remove characters, it doesn't reduce the capacity unless shrinktofit is utilized in the string.
- If the string exceeds its capacity, it automatically expands to decrease frequent memory reallocations.
- The string capacity is never smaller than the length of the string.
- It helps us to easily understand and control how much memory a string takes during operations.
Conclusion
In summary, the capacity method in C++ offers visibility into the allocated memory for a string, indicating the potential for growth before necessitating reallocation. This feature aids in improving efficiency by minimizing the need for frequent memory adjustments during string manipulations. With the capacity expanding automatically, we have the option to manage it manually through reserve or decrease excess memory with shrinktofit. Altogether, the capacity function serves as a valuable tool for enhancing string memory utilization in C++.
C++ String capacity Function FAQs
1) What is the string capacity function in C++?
In C++, the capacity method in strings provides the maximum number of characters the string can hold without needing to reallocate memory. This value is guaranteed to be equal to or higher than the current size of the string.
No, the capacity function in C++ does not always equal the size of the string.
No, the capacity method doesn't necessarily match the size of the string in C++. It actually exceeds or equals the string's size. C++ allocates additional memory to facilitate efficient program expansion.
Yes, it is not possible to decrease the capacity of a string in C++.
No, it is not possible to reduce the size of a string in C++. The string does not automatically decrease in size. Nevertheless, invoking the shrinktofit method can be used to suggest a reduction in capacity, although it is not guaranteed.
Is the capacity consistently larger than or equal to the size?
Yes, the ```
str.capacity;
5) What sets the capacity() function apart from equivalents in different systems or compilers in C++?
The behavior of the capacity() function in C++ varies across different systems and compilers due to the implementation-specific string memory allocation strategy. This results in potential variations in the allocated capacities by different compilers within a C++ program.