In this post, you’ll discover what wstring is. How can we use long strings? What is the distinction between string and wstring? By learning wide strings (wstring) and terms, it will help you to build C++ applications with the use of a C++ IDE.
Generally, in Introduction to C++ lessons, examples start with string examples and end with them, while Modern C++ uses Wide Strings and Unicode Strings to support worldwide languages. In C++ there are several typedefs for common character types that are provided: String types are defined in header <string>.
Table of Contents
Here are the string types defined in std namespace with their char type and C++ standard
String Type | Char Type | Definition | C++ Standard |
std::string | char | std::basic_string<char> | C++ |
std::wstring | wchar_t | std::basic_string<wchar_t> | C++ |
std::u8string | char8_t | std::basic_string<char8_t> | C++20 |
std::u16string | char16_t | std::basic_string<char16_t> | C++11 |
std::u32string | char32_t | std::basic_string<char32_t> | C++11 |
Here are the string types defined in std::pmr namespace with their char type and C++ standard
String Type | Char Type | Definition | C++ Standard |
std::pmr::string | char | std::pmr::basic_string<char> | C++17 |
std::pmr::wstring | wchar_t | std::pmr::basic_string<wchar_t> | C++17 |
std::pmr::u8string | char8_t | std::pmr::basic_string<char8_t> | C++20 |
std::pmr::u16string | char16_t | std::pmr::basic_string<char16_t> | C++17 |
std::pmr::u32string | char32_t | std::pmr::basic_string<char32_t> | C++17 |
What you need to know about wide strings in C++
Wide strings are the string class for wide characters represented with wstring and alphanumeric characters are stored and displayed in string forms. In another terms wstring stores for the alphanumeric text with 2 or 4 byte chars. Wide strings are the instantiation of the basic_string class template that uses wchar_t as the character type. Simply we can define a wstring as below,
When we print out wide strings we must use wcout command. Here is a very simple example to use wide strings,
Reading wide strings from STDIN in C++
We can get wstrings with wcin command as given example below,
How to print wide strings in C++
We can use printf() function as below,
This is the way to get the length, size and other properties of a C++ wide string
We can use length(), size(), max_size() methods of wstring class to get length, size and max_size() of wide string. See example below,
What else should I know about wide strings in C++?
In Modern C++ , strings and wide strings can be used, we highly recommend you to use unicode strings. Unicode standard for UnicodeString provides a unique number for every character (8, 16 or 32 bits) more than ASCII (8 bits) characters. UnicodeStrings are being used widely because of support to languages world wide and emojis. In modern C++ nowadays there are two types of strings used; array of chars (char strings) and UnicodeStrings (WideStrings and AnsiStrings are older, not compatible with all features now). CLANG / C++ Builder / GNU C / VC++ compilers, IDEs are using this standard for GUI forms to support all languages to provided applications in global. More information about the structure of Unicode Strings can be found here . RAD Studio , Delphi & C++ Builder uses Unicode-based strings: that is, the type String is a Unicode string (System.UnicodeString) instead of an ANSI string. If you want to transform your codes to Unicode strings we recommend you this article.
Please check this post about Unicode Strings in C++ On Windows