Function Argument with std::vector

Function Argument with Vector

Here is a program that requires a function to print each element of a std::vector.

Make the function and update the program to work.

#include <iostream>
#include <vector>

// Function declaration goes here

int main()
{
    std::vector<int> arr = { 10, 20, 30, 40, 50 };
    printVector(arr); // Calling the print function
}

Answer

#include <iostream>
#include <vector>

// Function to print elements of a std::vector
void printVector(std::vector<int> vec) 
{
    for (size_t i = 0; i < vec.size(); i++) 
    {
        std::cout << vec[i] << "\n";
    }
}

int main() 
{
    std::vector<int> arr = {10, 20, 30, 40, 50};
    printVector(arr); // Calling the print function
}
Questions