How to use front_inserter to append all existing list elements to the same list in C++

1 Answer

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

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

void print(const list<int>& lst)
{
	for (auto element : lst) {
		cout << element << ' ';
	}
	cout << endl;
}

int main()
{
	list<int> lst;

	front_inserter(lst) = 1;
	front_inserter(lst) = 2;
	front_inserter(lst) = 3;
	print(lst);

	copy(lst.begin(), lst.end(),  front_inserter(lst));
	print(lst);
}


/*
run:

3 2 1
1 2 3 3 2 1

*/

 



answered Jan 23, 2018 by avibootz
edited Jan 24, 2018 by avibootz

Related questions

1 answer 155 views
1 answer 187 views
3 answers 287 views
1 answer 214 views
2 answers 261 views
1 answer 201 views
...