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.
1 2 3 |
for (auto vi= vec.begin(); vi!= vec.end(); vi++) std::cout << *vi << ','; |
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
.
1 2 3 |
for (auto vi= vec.rbegin(); vi!= vec.rend(); vi++) std::cout << *vi << ','; |
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
.
1 2 3 |
for (auto vi= vec.cbegin(); vi!= vec.cend(); vi++) std::cout << *vi << ','; |
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
.
1 2 3 |
for (auto vi= vec.crbegin(); vi!= vec.crend(); vi++) std::cout << *vi << ','; |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <iostream> #include <string> #include <vector> int main() { // A vector in strings std::vector<std::string> vec{ "Bruneau", "David", "Ian", "Marco", "Nuno", "Yilmaz" }; // standard iteration for (auto v: vec) std::cout << v << ','; std::cout << std::endl << std::endl; // iteration with begin/end // std::vector<std::string>::iterator vi; for (auto vi= vec.begin(); vi!= vec.end(); vi++) std::cout << *vi << ','; std::cout << std::endl; // reverse iteration with rbegin/rend for (auto vi= vec.rbegin(); vi!= vec.rend(); vi++) std::cout << *vi << ','; std::cout << std::endl << std::endl; // constant iteration with cbegin/cend for (auto vi= vec.cbegin(); vi!= vec.cend(); vi++) std::cout << *vi << ','; std::cout << std::endl; // constant reverse iteration with crbegin/crend for (auto vi= vec.crbegin(); vi!= vec.crend(); vi++) std::cout << *vi << ','; std::cout << std::endl; system("pause"); return 0; } |
and the output will be as follows:
1 2 3 4 5 6 7 8 9 |
Bruneau,David,Ian,Marco,Nuno,Yilmaz, Bruneau,David,Ian,Marco,Nuno,Yilmaz, Yilmaz,Nuno,Marco,Ian,David,Bruneau, Bruneau,David,Ian,Marco,Nuno,Yilmaz, Yilmaz,Nuno,Marco,Ian,David,Bruneau, |
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.