Site icon Learn C++

Using Iterators of String in C++ Software

How can I use Iterator methods of strings in my C++ software apps? What kind of iterator methods I can use with a std::string? How can I use begin(), end() iterator methods with strings? the at() method of strings? Can I use front() and back() methods in std::string to access characters?

Modern C++ uses Strings, Wide Strings, and Unicode Strings to support worldwide languages. Strings (std::string) uses char as the character type which means they are ASCII chars and they are an instantiation of the basic_string class template. In C++, there are several typedefs for common character types and one of them is std::string types that are defined in header <string>.

strings are the string class for byte characters represented with string and alphanumeric characters are stored and displayed in string forms. The string class stores the alphanumeric text with 1-byte chars called char. Strings are the instantiation of the basic_string class template that uses char as the character type. In modern C++, simply we can define a string as below,

[crayon-673fd2e2c14f7031040470/]

The string 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 characters of a string with Iterators; we can use begin()end() methods to define range. 

How to use iterators of a String in C++ software

Strings (string) are a class contains arrays of characters with useful methods, and we can access, or modify their characters easily. In C++, while string contents are defined between ” and “, characters are defined between ‘ and ‘.

Iterator (<iterator>) is an object that points an element in a range of elements (i.e. characters of a 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 String with Iterators and the begin(), end() methods

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

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

[crayon-673fd2e2c1506007941291/]

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

[crayon-673fd2e2c1508194448107/]

as you see in this example we print each characters of a string by using iterators of the string.

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

The Iterator rbegin() is a String Method, a reverse iterator (reverse_iterator) that points the last character of the string. The Iterator rend() is a String method, a reverse iterator that points the first character of the string. We can use for() to iterate characters of a string 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-673fd2e2c150a831598105/]

Here auto i is same with std::string::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-673fd2e2c150b105484219/]

as you see in this example we print each characters of a string by using iterators of the string.

How to access a character of a String with constant iterators and the cbegin(), cend() methods

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

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

[crayon-673fd2e2c150c572999023/]

Here auto i is same with std::string::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-673fd2e2c1518344322797/]

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

How to address a character of a String with constant reverse iterators and the crbegin(), crend() methods

The Iterator crbegin() is a String Method, a constant reverse iterator (const_reverse_iterator) that points the last character of the string. The Iterator crend() is a String Method, a constant reverse iterator (const_reverse_iterator) that points the first character of the string. We can use for() to iterate characters of a string 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 crbegin() and crend() iterators,

[crayon-673fd2e2c1519092599219/]

Here auto i is same with std::string::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-673fd2e2c151e678726442/]

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

Exit mobile version