Site icon Learn C++

What Are The New Begin End Iterators In C++14?

What Are The New Begin End Iterators In C++14

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::beginstd::end functions, and in this post, we explain these new begin-end iterators.

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,

Here is a simple example how we can use begin end iterators in the for iteration as below.

[crayon-6742be688c7cb622779348/]

What are the reverse begin end iterators in C++14?

In C++14 , there are new additions to the global std::beginstd::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.

Here is an example with a vector vec.

[crayon-6742be688c7d3693457795/]

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.

Here is an example with a vector vec.

[crayon-6742be688c7d5540333534/]

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,

Here is an example with a vector vec.

[crayon-6742be688c7d6961348584/]

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.

[crayon-6742be688c7d7038892229/]

and the output will be as follows:

[crayon-6742be688c7d8182040374/]

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.

Exit mobile version