How to iterate through a vector in C++?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Iterating through a vector in C++ can be done using several methods, each serving different needs and preferences. A vector in C++ is a dynamic array provided by the Standard Template Library (STL). Here, we'll explore some common techniques to iterate through a vector using C++.

1. Using a Traditional for Loop

For simple access where you might also need the index of the elements, a traditional for loop is very straightforward:

#include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; for (size_t i = 0; i < vec.size(); ++i) { std::cout << vec[i] << " "; } return 0; }

2. Using a Range-based for Loop (C++11 and later)

For simpler syntax and direct access to elements without needing an index, the range-based for loop introduced in C++11 is ideal:

#include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; for (int num : vec) { std::cout << num << " "; } return 0; }

3. Using Iterators

Iterators provide a flexible way to iterate through a vector. They are especially useful when you need to perform more complex operations like modifying the container during iteration.

#include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; for (auto it = vec.begin(); it != vec.end(); ++it) { std::cout << *it << " "; } return 0; }

4. Using std::for_each with Lambda Function (C++11 and later)

The std::for_each algorithm from the <algorithm> header can be used along with a lambda function to apply a function to all elements in a vector.

#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; std::for_each(vec.begin(), vec.end(), [](int num) { std::cout << num << " "; }); return 0; }

5. Using C++20 Ranges (C++20 and later)

If you are using C++20, you can use ranges to iterate over a vector. This requires the <ranges> header.

#include <iostream> #include <vector> #include <ranges> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; for (int num : std::ranges::views::all(vec)) { std::cout << num << " "; } return 0; }

Choosing the Right Method

  • Traditional for Loop: Best when you need index-based access.
  • Range-based for Loop: Best for readability and simplicity when you don't need the element index.
  • Iterators: Most flexible, essential for STL-compatible code and when modifying the container.
  • std::for_each and Lambdas: Great for applying a function to each element, offers clean syntax with lambda expressions.
  • C++20 Ranges: Modern, readable, and flexible, especially useful with the new standard libraries.

Each of these methods has its place depending on the specific requirements of your code and your personal or team coding standards.

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Blind 75 vs. NeetCode 150 vs. Educative vs. Design Gurus - which is better?
How to get selected in Intel?
What is the hardest thing to learn in coding?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Image
Grokking Data Structures & Algorithms for Coding Interviews
Image
Grokking Advanced Coding Patterns for Interviews
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.