How can I use wide string methods in C++ software? What kind of methods I can use to get the size of a std::wstring? How can I use the empty() method with wstrings? How to get length() of a std::wstring? How can I use the size() property of a std::wstring? How can I use the max_size() property of a wstring? How can I retrieve the capacity of a wstring? How can I use the reserve() method with wstring? How to use the shrink_to_fit() method of wstring?
Modern C++ uses Wide Strings and Unicode Strings to support worldwide languages. Wide Strings (std::wstring) uses wcha_tr as the character type which means they are 2-bytes chars and they are an instantiation of the basic_string class template. In C++, there are several type definitions for common character types and one of them is std::wstring types that are defined in header <string>.
Table of Contents
What is a WString or Wide String in C++ Software?
wstrings are the string class for 2-bytes characters represented with wstring and alphanumeric characters are stored and displayed in wide string forms. In other terms, wstring stores for the alphanumeric text with 2-byte chars, called wchar_t. Wide Strings are the instantiation of the basic_string class template that uses wchar_t as the character type. In modern C++, simply we can define a wide string with L” ” literal as below,
1 2 3 |
std::wstring wstr = L"This is a Wide String"; |
Wide Strings (wstring) are a class that contains arrays of wide characters with useful methods, and we can access, or modify their characters easily. In C++, while wide string contents are defined between L” and ” with literal L, wide characters are defined between L’ and ‘ with literal L
The wstring 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.
How to Use empty() method in C++ software
empty() method is a String Method that checks if wstring 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::wstring wstr = L"This is a Wide String"; if( wstr.empty() ) std::wcout << L"Yes" << std::endl; else std::wcout << L"No" << std::endl; |
The length() and size() properties in C++ software
length() method and size() method are string methods that returns the number of characters (CharT) in a wstring.
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::wstring str = L"This is a Wide String"; std::wcout << L"Length:" << str.length() << std::endl; std::wcout << L"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++ software
max_size() method is a String Method that returns the maximum number characters for a wstring 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 wstring 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::wstring str = L"This is a Wide String"; std::wcout << L"Max Size:" << wstr.max_size() << std::endl; |
Learn to Use capacity() method in C++
capacity() method is a String Method that returns the currently allocated storage that the wstring.
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::wstring wstr = L"This is a Wide String"; std::wcout << L"Capacity:" << wstr.capacity() << std::endl; |
What is the Use reserve() method in C++?
reserve() method is a String Method that reserves a and defines a new capacity for the wstring.
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::wstring wstr = L"This is a Wide String"; wstr.reserve(50); std::wcout << L"Size:" << wstr.size() << std::endl; std::wcout << L"Capacity:" << wstr.capacity() << std::endl; |
As an output Size will be 16 and Capacity will be 31 that means there is no effect to reserve.
This is how to use the shrink_to_fit() method in C++ software
shrink_to_fit() method is a String Method that requests the removal of unused capacity of the wstring.
1 2 3 |
void shrink_to_fit();; // C++11 to C++20 |
Here is an example
1 2 3 4 5 6 7 |
std::wstring wstr = L"This is a Wide String"; wstr.shrink_to_fit(); std::wcout << L"Size:" << wstr.size() << std::endl; std::wcout << L"Capacity:" << wstr.capacity() << std::endl; |
An example of how to get Information about a Wide String with its properties in a C++ app
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 wstr_info(std::wstring ws) { auto cap = ws.capacity(); auto size = ws.size(); auto length = ws.length(); auto max_size = ws.max_size(); std::wcout << L"WideString: " << ws << std::endl; std::wcout << L" length : " << length << std::endl; std::wcout << L" size : " << size << std::endl; std::wcout << L" max-size: " << max_size << std::endl; std::wcout << L" capacity: " << cap << std::endl; std::wcout << L" empty : "; if(ws.empty()) std::wcout << L"Yes" << std::endl; else std::wcout << L"No" << std::endl; std::wcout << std::endl; } //------------------------------------------------------------------------------ int main() { std::wstring wstr = = L"This is a Wide String"; wstr_info(wstr); getchar(); return 0; } |
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition