C++C++11C++14C++17Introduction to C++Learn C++

Learn To Use Wide Strings (wstring) In C++

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>.

Here are the string types defined in std namespace with their char type and C++ standard

String TypeChar TypeDefinitionC++ Standard
std::u16string char16_tstd::basic_string<char16_t>C++11
std::u32string char32_tstd::basic_string<char32_t>C++11

Here are the string types defined in std::pmr namespace with their char type and C++ standard

String TypeChar TypeDefinitionC++ Standard
std::pmr::wstring wchar_tstd::pmr::basic_string<wchar_t>C++17
std::pmr::u16string char16_tstd::pmr::basic_string<char16_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

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

Learn Default Constructors Of Classes In Modern C++

Artificial Intelligence TechC++C++11C++14C++17Learn C++

How To Develop Special AI Activation Functions In C++?

Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?