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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

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.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.