C++C++14C++17IteratorsLearn 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,

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

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

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

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

Learn Default Constructors Of Classes In Modern C++

Artificial Intelligence TechC++C++11C++14C++17Learn C++

How To Develop Special AI Activation Functions In C++?

Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?