#include <iostream>
and #include <vector>
: Include necessary libraries for input/output and vector functionalities.
std::vector<int> createDynamicVector(int n)
: Declare a function named createDynamicVector
that takes an integer n
as an argument and returns a vector of integers.
std::vector<int> dynamicVector;
: Declare an empty vector called dynamicVector
capable of holding integers.
for (int i = 1; i <= n; ++i) { dynamicVector.push_back(i 10); }
: Populate the dynamicVector
with elements (multiples of 10) from 10 to n10
using a for loop.
return dynamicVector;
: Return the populated vector from the createDynamicVector
function.
int main() { ... }
: Define the main function.
int size = 5;
: Initialize an integer variable size
with a value of 5.
std::vector<int> result = createDynamicVector(size);
: Call the createDynamicVector
function passing size
as an argument and store the returned vector in result
.
std::cout << "Dynamic Vector elements:" << std::endl;
: Display a message indicating the following output will be the elements of the dynamic vector.
for (int i = 0; i < result.size(); ++i) { std::cout << result[i] << " "; }
: Iterate through the elements of the result
vector and print each element followed by a space.
return 0;
: Indicates the end of the main()
function and the successful execution of the program.