problem with list<T>::iterator

who can help me
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#include <iostream>
#include <list>
using namespace std;


class myclass
{
	int i;
	public:
	void inti(int i)
	{
		this->i=i;
	}
	void display()
	{
		cout<<"i= "<<i<<endl;
		
	}
};

int main(int argc, char** argv)
{
	myclass mc;
	mc.inti(10);
	list<myclass> classlist;
	classlist.push_back(mc);
	list<myclass>::iterator end=classlist.end();
	end->display();
	
	return 0;
}

the output is not i=10 why is that?
vector<>::end() does mot point to the last object in the queue. It points to one object past the last one. If you want to get the last object use vector<>::back().
Because the member function end() doesn't point to the last element of the list but to the next after the last.

Instead of end->display(); try this (--end)->display();
vector<>::end() does mot point to the last object in the queue. It points to one object past the last one. If you want to get the last object use vector<>::back().

hamsterman was quicker as I see.
thank you so much
Topic archived. No new replies allowed.