Runtime error: Cannot dereference end list iterator

Apr 19, 2020 at 6:28am
I am trying to implement quick sort with the median of three as my pivot. My list is of key value pairs, I am trying to save the middle key in order to compare it with the first and last, however,whenever I get my iterator to the middle of the list, I get an error.

This is how I am declaring my typename/typedef for reference

1
2
3
4
5
6
template <typename K, typename V>
class Sorting {
public:
	typedef Entry<K, V> E;
	typedef std::list<E> List;
	typedef typename List::iterator it;


This is the piece of code I am getting the error on

1
2
3
4
5
6
it itPos = quickList.begin();
std::advance(itPos, quickList.size() / 2);
K mid = (*itPos).key();
K front = (*quickList.begin()).key();
K back = (*quickList.end()).key();
K p;


quickList is a List object

I receive the error immediately after the std::advance line. I am not sure why I am getting this error, when looking at the locals, I am at the correct position in the list, but I am not sure why I get the error. Any suggestions as to what is going wrong?
Last edited on Apr 19, 2020 at 6:30am
Apr 19, 2020 at 6:42am
The end() iterator points behind the container so you can't reference it, you only compare it to some other iterator.
http://www.cplusplus.com/reference/list/list/end/
Apr 19, 2020 at 6:46am
Ahh okay! I used back() instead and it worked. Thanks for the help!
Topic archived. No new replies allowed.