How to fill two empty lists 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> list1, list2;

	for (int i = 0; i < 5; i++) {
		list1.push_back(i);
		list2.push_back(i * 2);
	}

	printList(list1);
	printList(list2);

	return 0;
}

/*
run:

0 1 2 3 4
0 2 4 6 8

*/

 



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

Related questions

1 answer 188 views
188 views asked Jan 5, 2018 by avibootz
1 answer 166 views
166 views asked Dec 30, 2017 by avibootz
1 answer 182 views
1 answer 186 views
186 views asked Apr 21, 2018 by avibootz
1 answer 202 views
...