How to iterator from element before last (second last) to second element of a list in C++

1 Answer

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

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

int main()
{
	list<int> lst{ 1, 3, 8, 23, 88, 12, 99, 7 };
	
	for (list<int>::iterator i = lst.begin(), j = prev(--lst.end()); j != i; j--) {
		cout << *j << ' ';
	}

	cout << endl;

}


/*
run:

99 12 88 23 8 3

*/

 



answered Jan 20, 2018 by avibootz
...