How to print list with for_each in C++

1 Answer

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

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

void print(int n) {
	cout << n << " ";
}

int main()
{
	list<int> lst{ 13, 12, 300, 1, 98 };

	for_each(lst.begin(), lst.end(), print);

	cout << endl;

	return 0;
}

/*
run:

13 12 300 1 98

*/

 



answered Apr 27, 2018 by avibootz

Related questions

1 answer 169 views
2 answers 293 views
293 views asked Dec 31, 2017 by avibootz
1 answer 107 views
1 answer 166 views
1 answer 187 views
...