How to initialize and print a list with ints in C++

1 Answer

0 votes
#include <iostream>  
#include <list> 

using std::list;
using std::cout;

void printList(const list<int>& lst)
{
	for (auto elem : lst) {
		cout << elem << ' ';
	}
	cout << std::endl;
}

int main()
{
	list<int> lst{ 1, 2, 3, 4, 5 };

	printList(lst);

	return 0;
}

/*
run:

1 2 3 4 5

*/

 



answered Jan 5, 2018 by avibootz
edited Jan 7, 2018 by avibootz

Related questions

1 answer 168 views
1 answer 187 views
4 answers 338 views
2 answers 223 views
1 answer 168 views
1 answer 150 views
...