Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::wstring — a string of wchar_t elements

std::wstring is an alias for std::basic_string<wchar_t>, defined in <string>. It manages string storage and supports appending, comparison, and element access. It does not guarantee a portable Unicode encoding.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

#include <string>
std::wstring text = L"Hello";

std::wstring is an alias for std::basic_string<wchar_t> from <string>. It stores a sequence of wchar_t elements contiguously, manages their lifetime, and allows the string length to change. The L prefix creates wide literals: L"cat" is a string literal and L'C' is a character literal.

size() returns the number of stored wchar_t elements, excluding the terminating zero element. This is not generally the number of Unicode code points or user-perceived characters. The width of wchar_t and the encoding of wide literals depend on the implementation; std::wstring guarantees neither UTF-16 nor UTF-32. It also does not automatically convert text from std::string.

The += operator appends text, while at(i) provides bounds-checked element access and throws std::out_of_range when i >= size(). The c_str() member exposes a pointer to zero-terminated data, useful for interfaces accepting const wchar_t*. A string can also contain embedded zeros, but an interface relying solely on a terminator will treat the first one as the end of the text.

The example creates L"cat", appends L"s", and changes the first element. It uses std::cout to print only the length and comparison result, so it does not require rendering wide text in the terminal. The expected lines are 4 and 1. This description covers operations available in C++23; the supplied source is an evolving standard draft rather than a frozen C++23 publication.

Examples

#include <iostream>
#include <string>

int main() {
    std::wstring text = L"cat";
    text += L"s";
    text.at(0) = L'C';

    std::cout << text.size() << '\n';
    std::cout << (text == L"Cats") << '\n';
}

Result

4
1

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources