How to swap between first and last value of a list in C++

1 Answer

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

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{ 1, 3, 8, 23, 88, 12, 99, 7 };

	iter_swap(lst.begin(), prev(lst.end()));

	print(lst);

}


/*
run:

7 3 8 23 88 12 99 1

*/

 



answered Jan 21, 2018 by avibootz

Related questions

1 answer 152 views
1 answer 166 views
3 answers 247 views
1 answer 184 views
2 answers 219 views
...