#include<list>
#include <iostream>
usingnamespace 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.
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.
#include<list>
#include <iostream>
usingnamespace 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;
}
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!