How can I use the string capacity and length methods in a C++ app? What kind of methods I can use to get size of a std::string? How can I use the empty() method with strings? How to get length() of a std::string? How can I use size() property of a std::string? How can I use max_size() property of a std::string? How can I retrieve capacity of a string? How can I use reserve() method ? How to use shrink_to_fit() method?
Table of Contents
What sort of string methods are available to a C++ app?
Modern C++ uses Strings, Wide Strings, and Unicode Strings to support worldwide languages. Strings (std::string) uses char as the character type which means they are ASCII chars and they are an instantiation of the basic_string class template. In C++, there are several typedefs for common character types and one of them is std::string types that are defined in header <string>.
strings are the string class for byte characters represented with string and alphanumeric characters are stored and displayed in string forms. In other terms, string stores the alphanumeric text with 1-byte chars called char. Strings are the instantiation of the basic_string class template that uses char as the character type. In modern C++, simply we can define a string as below,
1 2 3 |
std::string str = "This is a String"; |
Strings (string) are a class contains arrays of characters with useful methods, and we can access, or modify their characters easily. In C++, while string contents are defined between ” and “, characters are defined between ‘ and ‘.
The string has methods to append, assign, copy, align, replace or operate with other strings. These methods can be used in all string methods with their appropriate syntax. We can access to capacity properties of a string with size(), length(),_max_size(), capacity(), empty(), reserve(), shrink_to_fit() methods.
What does the capacity property and empty methods of a String actually do?
Learning to use empty() Method in C++
empty() method is a String Method that checks if string has no character, in other terms its size is 0 or it is equal to “”. It checks if begin() iterator is equal to end() itarator.
Syntax:
1 2 3 |
bool empty() const noexcept; // C++11 to C++20 |
empty() method example,
1 2 3 4 5 |
std::string str = "This is a String"; if( str.empty() ) std::cout << "Yes" << std::endl; else std::cout << "No" << std::endl; |
What does the String length() and size() properties do in a C++ app?
length() method and size() method are string methods that returns the number of characters (CharT) in a string.
Syntax:
1 2 3 |
size_type length() const noexcept; // C++11 to C++20 |
1 2 3 |
size_type size() const noexcept; // C++11 to C++20 |
Here is an example about length() and size() methods
1 2 3 4 5 |
std::string str = "This is a String"; std::cout << "Length:" << str.length() << std::endl; std::cout << "Size:" << str.size() << std::endl; |
As we know both methods are same, there is no difference in results.
Learn to use max_size() method in C++
max_size() method is a String Method that returns the maximum number characters for a string is able to hold due to system or library implementation limitations. For example this max_size() can be 4294967294, which means you can create a string in that maximum number of characters.
Syntax:
1 2 3 |
size_type max_size() const noexcept; // C++11 to C++20 |
Here is an example about using max_size() method,
1 2 3 4 |
std::string str = "This is a String"; std::cout << "Max Size:" << str.max_size() << std::endl; |
Learn to use C++ capacity() method
capacity() method is a String Method that returns the currently allocated storage that the string.
Syntax:
1 2 3 |
size_type capacity() const noexcept; // C++11 to C++20 |
Here is an example about using max_size() method,
1 2 3 4 |
std::string str = "This is a String"; std::cout << "Capacity:" << str.capacity() << std::endl; |
What does the C++ String reserve() method do?
reserve() method is a String Method that reserves a and defines a new capacity for the string.
1 2 3 |
void reserve( size_type newcapacity); // Until C++20 |
reserve method checks the current capacity and size of string,
– If newcapacity parameter is greater than the current capacity(), new storage is allocated, and capacity() is set to this newcapacity.
– If newcapacity parameter is less than it’s current capacity(), this is a non-binding shrink request.
– If newcapaicty parameter is less than it’s current size(), this is a non-binding shrink-to-fit request equivalent to shrink_to_fit()
– If newcapacity is less than or equal to the current capacity(), there is no effect.
Here is an example,
1 2 3 4 5 6 7 |
std::string str = "This is a String"; str.reserve(50); std::cout << "Size:" << str.size() << std::endl; std::cout << "Capacity:" << str.capacity() << std::endl; |
As an output Size will be 16 and Capacity will be 31 that means there is no effect to reserve.
How to use shrink_to_fit() method in a C++ app
shrink_to_fit() method is a String Method that requests the removal of unused capacity of the string.
1 2 3 |
void shrink_to_fit();; // C++11 to C++20 |
Here is an example
1 2 3 4 5 6 7 |
std::string str = "This is a String"; str.shrink_to_fit(50); std::cout << "Size:" << str.size() << std::endl; std::cout << "Capacity:" << str.capacity() << std::endl; |
How to get Information about a String with string properties
Let’s prepared a str_info() function to get information about a string. Here is a full example about to get string properties and about to use other capacity methods,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <iostream> #include <cassert> #include <string> //------------------------------------------------------------------------------ void str_info(std::string s) { auto cap = s.capacity(); auto size = s.size(); auto length = s.length(); auto max_size = s.max_size(); std::cout << "String : " << s << std::endl; std::cout << " length : " << length << std::endl; std::cout << " size : " << size << std::endl; std::cout << " max-size: " << max_size << std::endl; std::cout << " capacity: " << cap << std::endl; std::cout << " empty : "; if(s.empty()) std::cout << "Yes" << std::endl; else std::cout << "No" << std::endl; std::cout << std::endl; } //------------------------------------------------------------------------------ int main() { std::string str= "This is a String"; str_info(str); getchar(); return 0; } |