Iterators are one of the most useful features of containers in modern C++. Mostly we use them with vectors, maps, strings, and other containers of C++. In C++11, the begin()
and end()
iterators are used to define the start of iteration and the end of the iteration, mostly used in the for loops. In C++14, there are new additions to the global std::begin
– std::end
functions, and in this post, we explain these new begin-end iterators.
Table of Contents
What are the begin end iterators in C++11 and beyond?
In modern C++, containers are data storage arrays. They are very useful for iterating and searching data with their amazing methods and properties. An iterator (<iterator>) is an object that points to an element in a range of elements (i.e. characters of a string or members of a vector). We can use Iterators to iterate through the elements of this range using a set of operators, for example using the ++, –, and * operators.
Iteration can be done with begin/end iterators,
- The
begin()
method returns an iterator pointing to the first element in the vector. - The
end()
method returns an iterator pointing to the theoretical element that follows the last element in the vector.
Here is a simple example how we can use begin end iterators in the for iteration as below.
What are the reverse begin end iterators in C++14?
In C++14 , there are new additions to the global std::begin
– std::end
functions, they were augmented with reverse iterators, std::rbegin
and std::rend
. These are available in C++17 too.
Reverse iteration can be done with rbegin
and rend
iterators.
- The
rbegin()
method returns a reverse iterator pointing to the last element in the vector. It moves from last to first element. - The
rend()
method returns a reverse iterator pointing to the theoretical element preceding the first element in the vector.
Here is an example with a vector
vec
.
this list vector members in reverse order.
What are the constant begin end iterators in C++14?
In C++14 and C++17, there are also constant begin end iterators. std::cbegin
and std::cend
, and reverse iterators std::rbegin
/std::rend
and std::crbegin
and std::crend
Constant iteration can be done with with cbegin/cend iterators, this can be faster than normal iteration.
- The
cbegin()
method returns a constant iterator pointing to the first element in the vector. - The
cend()
method returns a constant iterator pointing to the theoretical element that follows the last element in the vector.
Here is an example with a vector
vec
.
this list vector members in constant iterators.
What are the constant reverse begin end iterators in C++14?
In C++14 and C++17, constant reverse iterations can be done with crbegin
and crend
iterators,
- The crbegin() method returns a constant reverse iterator pointing to the last element in the vector. It moves from last to first element.
- The crend() method returns a constant reverse iterator pointing to the theoretical element preceding the first element in the vector.
Here is an example with a vector
vec
.
this list vector members as a constant iterator in reverse order.
Is there a full example about new begin end iterators in C++14?
Here is a full example about how we can use new begin end iterators in C++ and beyond.
and the output will be as follows:
Note that these can be applied to the other containers of the modern C++, such as strings
, arrays
, lists
, maps
, sets
, etc.
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.