How to insert a list into other list at specific position in C++

1 Answer

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

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

int main()
{
	list<int> lst1{ 1, 2, 3, 4, 5 }, lst2{100, 200};
	list<int>::iterator it = lst1.begin();
	
	advance(it, 3);

	std::insert_iterator< std::list<int> > ins_it(lst1, it);
	
	std::copy(lst2.begin(), lst2.end(), ins_it);

	for (it = lst1.begin(); it != lst1.end(); it++)
		cout << *it << ' ';

	cout << endl;

	return 0;
}

/*
run:

1 2 3 100 200 4 5

*/

 



answered Apr 27, 2018 by avibootz

Related questions

1 answer 157 views
1 answer 223 views
1 answer 186 views
2 answers 240 views
1 answer 175 views
1 answer 177 views
...