How to use inserter to insert all elements of a set into a list from second element in C++

1 Answer

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

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

template <typename T>
inline void print(const T &data)
{
	for (const auto &element : data) {
		cout << element << ' ';
	}
	cout << endl;
}

int main()
{
	set<int> st;

	std::insert_iterator<set<int> > itr(st, st.begin());

	inserter(st, st.end()) = 1;
	inserter(st, st.end()) = 2;
	inserter(st, st.end()) = 3;
	print(st);

	list<int> lst{ 7, 8 };
	print(lst);
	copy(st.begin(), st.end(), inserter(lst, ++lst.begin()));
	print(lst);
}


/*
run:

1 2 3
7 8
7 1 2 3 8

*/

 



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

Related questions

1 answer 139 views
1 answer 139 views
2 answers 181 views
1 answer 163 views
...