This should do the job. The comments above are accurate.
#include <iostream>
#include <list>
int main() {
std::list<int> listOfInts{1,2,3};
for(auto element: listOfInts) {
std::cout << element << " ";
}
std::cout << std::endl;
return 0;
}
Here is an alternative solution which uses vector. The code is almost identical, except that I have added a random number generator to push 10 random integers between 1 and 10 onto the vector.
There was a lot of discussion about whether a list<> should be used. The differences between a c++ list and vector are discussed in many places, including here: thispointer.com list vs vector and here:stackoverflow-is it ever a good idea to use a list. Most importantly, a list favors quick insertions and removals from the middle of the list whereas the vector favors quick retrieval of values, quick searches, and just about everything else you may want to do with a container.
Generally, the list<> is assumed to be a doubly linked list whereas the vector is assumed to be a "dynamically" growing array. By default, the vector is probably preferred, but analytics is the only way to truly answer the question.
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
int main() {
srand((unsigned)time(0));
int random_integer = rand();
std::vector<int> vectorOfInts;
for(int i = 0; i < 10; i++) {
vectorOfInts.push_back(rand()%10 + 1);
}
for(auto element: vectorOfInts) {
std::cout << element << " ";
}
std::cout << std::endl;
return 0;
}