How to move the first element of a list to the end of the list 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> lst{ 1, 2, 3, 4, 5 };

	lst.splice(lst.end(), lst, lst.begin());

	printList(lst);

	return 0;
}

/*
run:

2 3 4 5 1

*/

 



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

Related questions

2 answers 273 views
273 views asked Nov 23, 2022 by avibootz
1 answer 224 views
1 answer 179 views
179 views asked Nov 22, 2022 by avibootz
1 answer 165 views
165 views asked Nov 22, 2022 by avibootz
...