auto keyword

#include <iostream>
#include <vector>

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5};

    // Using auto keyword to declare an iterator
    for (auto it = numbers.begin(); it != numbers.end(); ++it) {
        std::cout << *it << " ";
    }

    std::cout << std::endl;

    // Using auto keyword in a range-based for loop
    for (auto& num : numbers) {
        std::cout << num << " ";
    }

    return 0;
}

Explanation:

  1. Include necessary headers for input/output and vector operations.
  2. Define the main function.
  3. Create a vector named numbers and initialize it with values {1, 2, 3, 4, 5}.
  4. Use the auto keyword to declare an iterator (it) for the vector, iterating from the beginning to the end of the vector.
  5. In the first loop, print the elements pointed to by the iterator.
  6. Output a newline to separate the outputs of the two loops.
  7. Use the auto keyword in a range-based for loop to iterate over each element (num) in the vector by reference.
  8. Print each element in the second loop.