How to print the last 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 };
	
	list<int>::iterator pos = lst.end();

	advance(pos, -1);

	cout << *pos << endl;

}


/*
run:

7

*/

 



answered Jan 20, 2018 by avibootz

Related questions

1 answer 191 views
3 answers 396 views
1 answer 167 views
1 answer 131 views
...