C++ Iterate Vector

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

C++ vectors are a dynamic array that grows and reduces their size automatically when we insert or remove elements from them. Like an array in C++, vectors are stored in a contiguous way in memory and we can access values present in vectors using indexes. Iterating a vector means traversing every element present in the vector until a condition is met. There are several ways we can use to iterate a vector in C++ using indexing, range-based loops, iterators, and the for_each method.

How to Iterate Through a Vector in C++

Iterating a vector means traversing every element present in the vector until a condition is met. There are several ways we can use to iterate a vector in C++. Some of the ways that are discussed further in this article about C++ Iterate Vector are:

  • Iterators
  • Indexing
  • Range-based loops

Prerequisites

Vectors are defined in C++ STL to know more about vectors in C++ you can read the article about vectors in C++ before starting this article.

Different Methods of C++ Iterate through vectors

Let's see different methods that allow us to iterate through a vector in C++ using C++ Iterate Vector.

Using Indexing

We can use the for or the while loop to iterate through a vector which is also the most used way to iterate over a vector in C++. To access an element value present at an index, we use the variable name followed by the index enclosed in brackets (<array>[index]).

Output

Explanation

In this program, we have declared a vector arr of 5 distinct elements. The function printVector takes a vector as the function parameter. To iterate and print the elements of the vector we use the for loop. The variable size stores the length of the vector and we traverse the vector from index 0 to position size -1. We can also replace for loop with a while loop as shown in the code snippet below.

Using Range-based for loops

Let's learn how to use range-based for loops to iterate through a vector in C++ using an example.

Output

In the above example, we used the auto type to select each element present in the vector. This way using auto allow us to not select the data type of each of the elements in the vector.

This method is not just limited to printing the elements of the vector. We can use range-based for loops to perform logical operations. For example, take the look at the example mentioned below:

Output

As shown in the examples above, indices are not the only way we can iterate over a vector in C++. There exists another efficient method through which we can iterate a vector.

Here, itr is the value stored in the vector vector that we wish to traverse. If we want to update the values present in the vector we can use the & symbol before the variable name itr which allows us to change the values present in the vector.

Here, itr is the address to the values which are stored in the vector vector.

Using std::for_each

In C++, STL algorithms expose us to another function that can be used to iterate over all the elements of a vector that is, for_each(start, end, callback).

Here, the function parameters are as follows:

  • start - It is an iterator to the start of a range.
  • end - Iterator to the end of the range.
  • callback - It is a function that is applied to all the elements of the vector.

The for_each function can be used to iterate over each element of the vector from start to end and apply the callback function to each of the elements.

Output

Here, in this example, we are passing the vector arr and the lambda function that prints the elements present in the vector.

Using Iterators

C++ Iterate can be used to loop over all the elements stored in the vector. C++ vector class has two different functions vector::begin() and vector::end() that return iterator to the start and end of the vector. They can be used to iterate over a vector in C++.

Output

Conclusion

  • In this article, we learned different ways that can be used to iterate over a vector in C++.
  • Just like iterating an array, we can use indexes to iterate over elements of an array.
  • Ranged-based for loops are another efficient and easier method that C++ provides to iterate over a vector.
  • C++ STL library provides a for_each method that takes vector beginning, end, and a callback function that is applied to each element of the vector.
  • C++ vector class has two different functions vector::begin() and vector::end() that returns an iterator to the start and end of the vector.
  • C++ Iterate Vector can be used to loop over all the elements stored in the vector.