Site icon Learn C++

How To Use Wide String Iterators In Modern C++ Software

How To Use Wide String Iterators In Modern C++ Software

How can I access a character of a wstring? How can I use the at() method of wide strings in modern C++ software? Can I use front() and back() methods in std::wstring to access characters?

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

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,

[crayon-67435245b7210974877159/]

The wstring has methods to append, assign, copy, align, replace or operate with other wide strings. These methods can be used in all string methods with their appropriate syntax. We can access to wide characters of a wstring with Iterators; we can use begin()end() methods to define range . 

How to use Wide String iterators in C++ Software

Wide Strings (wstring) are a class 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.

Iterator (<iterator>) is an object that points an element in a range of elements (i.e. characters of a wide string). We can use Iterators to iterate through the elements of this range using a set of operators ( for example ++, –, * operators). We can use these iterators to iterate characters of strings.

How to access a character of a Wide String with iterators and begin(), end() methods in C++

The Iterator begin() is a Wide String Method, an iterator that points the first character of the string. The Iterator end() is a Wide String Method, an iterator that points the last character of the wide string. We can use for() to iterate characters of a wstring in the range, between the begin() and end() iterators.

This example below shows how we can print characters of a wstring by using iterator in the range, between the begin() and end() iterators,

[crayon-67435245b7218906069229/]

Here auto i is same with std::wstring::iterator i, we used begin() and end() iterators to define range of our for() loop . The wide string output of this example will be as follows,

[crayon-67435245b721a658256735/]

as you see in this example we print each characters of a wide string by using iterators of the wstring. Note that these each characters are wide characters, in 2-bytes form.

How to access a character of a Wide String with reverse iterators and rbegin(), rend() methods

The Iterator rbegin() is a Wide String Method, a reverse iterator (reverse_iterator) that points the last character of the string. The Iterator rend() is a Wide String Method, a reverse iterator that points the first character of the wide string. We can use for() to iterate characters of a wstring in the reverse range, between the rbegin() and rend() iterators.

This example below shows how we can print characters of a string by using reverse iterator in the range, between the rbegin() and rend() iterators,

[crayon-67435245b721c389800511/]

Here auto i is same with std::wstring::reverse_iterator i, we used rbegin() and rend() reverse iterators to define range of our for() loop . The string output of this example will be as follows,

[crayon-67435245b721e619882308/]

as you see in this example we print each characters of a string by using iterators of the string. Note that these each characters are wide characters , in 2-bytes form.

Use C++ to access a character of a Wide String with Constant Iterators and cbegin(), cend() methods

The Iterator cbegin() is a Wide String Method, a constant iterator (const_iterator) that points the first character of the string. The Iterator cend() is a Wide String Method, a constant iterator (const_iterator) that points the last character of the string. We can use for() to iterate characters of a wstring in the range, between the cbegin() and cend() iterators.

This example below shows how we can print characters of a wstring by using constant iterator in the range, between the cbegin() and cend() iterators,

[crayon-67435245b7220788006136/]

Here auto i is same with std::wstring::const_iterator i, we used cbegin() and cend() const_iterators to define range of our for() loop . The string output of this example will be as follows,

[crayon-67435245b7224432072781/]

as you see in this example we print each characters of a wide string by using const_iterator of the wstring. Note that const_iterator is faster than iterator and can not be changed.

Accessing a character from a Wide String with constant reverse iterators and the crbegin(), crend() C++ methods

The Iterator crbegin() is a Wide String Method, a constant reverse iterator (const_reverse_iterator) that points the last character of the wide string. The Iterator crend() is a Wide String Method, a constant reverse iterator (const_reverse_iterator) that points the first character of the wide string. We can use for() to iterate characters of a wstring in the reverse range, between the rbegin() and rend() iterators.

This example below shows how we can print characters of a wstring by using reverse iterator in the range, between the crbegin() and crend() iterators,

[crayon-67435245b7226218032553/]

Here auto i is same with std::wstring::const_reverse_iterator i, we used crbegin() and crend() constant reverse iterators to define range of our for() loop . The string output of this example will be as follows,

[crayon-67435245b7228085135791/]

as you see in this example, we print each characters of a wide string by using iterators of the wstring. Note that const_reverse_iterator is faster than reverse_iterator and can not be changed.

Exit mobile version