To create a list of numbers from 1 to N in C++ using std::list, you can use a for loop to add elements to the list.
In this example,
header to use the std::list container.numbers
and use a for loop to add numbers from 1 to N.#include <iostream>
#include <list>
int main() {
int N = 5;
std::list<int> numbers;
for (int i = 1; i <= N; ++i) {
numbers.push_back(i);
}
for (int num : numbers) {
std::cout << num << " ";
}
return 0;
}
1 2 3 4 5
In this tutorial, we learned How to create a List of Numbers from 1 to N in C++ language with well detailed examples.