loop through a vector in c++

#include <iostream>
#include <vector>

int main() {
    std::vector<int> numbers = {2, 4, 6, 8, 10};

    // Loop through the vector using a for loop
    for (size_t i = 0; i < numbers.size(); ++i) {
        std::cout << numbers[i] << " ";
    }

    return 0;
}
  1. Include Libraries: Include necessary libraries (iostream and vector).

  2. Main Function: Declare the main function.

  3. Vector Creation: Create a vector named numbers of integers and initialize it with values {2, 4, 6, 8, 10}.

  4. For Loop: Start a for loop to iterate through the vector.

  5. size_t i = 0;: Initialize a variable i as an unsigned integer and set it to 0.
  6. i < numbers.size();: Condition to check if i is less than the size of the numbers vector.
  7. ++i: Increment the value of i after each iteration.

  8. Access and Output: Inside the loop, access elements of the vector using numbers[i] and output each element followed by a space using std::cout.

  9. Return: End the main function and return 0, indicating successful execution.