Need help displaying a List using an iterator

So far I have

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
33
34
35
36
37
38
39
40
41
42
43
44

#include<list>
#include <iostream>

using namespace std;

int main()
{
	list<double> numList;

	//add the values 12.7, 9.65, 8.72 and 4.69 to numList
	numList.push_back(12.7);
	numList.push_back(9.65);
	numList.push_back(8.72);
	numList.push_back(4.69);


	cout << "numList contains: "<< endl;
	//display the list using an iterator



	list<double>::iterator itt = numList.begin();
	itt++;
	itt++;

	for(itt = numList.begin(); itt != numList.end(); itt++)
cout<<*itt<<" ";

	//remove the element pointed to by itt
	numList.erase(itt);

	//reverse the list
	numList.reverse();


	cout << "\nnow numList contains: " << endl;
	//display the list again

	

	return 0;
}


It compiles but soon after it compiles I get a message saying "Debug Assertion Failed"

After a display the list, I need to display it again at the bottom, but I cant do that till I have properly displayed it the first time.

ANy help would be greatly appreciated, Thank you!
numList.erase(itt); This is the problem. After the loop itt will point to numList.end() which is one past the last element. This is not a valid iterator to pass to erase.
Thank you. I commented out the numList.erase leaving me with
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46

#include<list>
#include <iostream>

using namespace std;

int main()
{
	list<double> numList;

	//add the values 12.7, 9.65, 8.72 and 4.69 to numList
	numList.push_back(12.7);
	numList.push_back(9.65);
	numList.push_back(8.72);
	numList.push_back(4.69);


	cout << "numList contains: "<< endl;
	//display the list using an iterator

	

	list<double>::iterator itt = numList.begin();
	itt++;
	itt++;

	for(itt = numList.begin(); itt != numList.end(); itt++)
	cout<<*itt<<" ";


	//remove the element pointed to by itt
	//numList.erase(itt);

	//reverse the list
	numList.reverse();


	cout << "\nnow numList contains: " << endl;
	//display the list again
	for(itt = numList.begin(); itt != numList.end(); itt++)
    cout<<*itt<<" ";
	

	return 0;
}


but how do I go about erasing the last element?
lists have a pop_back() function that deletes the last element.
numList.pop_back();

If you want to use the erase function you can take the end iterator and go to the previous element, that will be the last element.
1
2
3
itt = numList.end();
--itt;
numList.erase(itt);
thank you, probably would have been easier had I used the pop_back() function, but I like the idea of the end iterator and having it point to the previous element and erasing it that way. Thanks again!
Topic archived. No new replies allowed.