-
April 1st, 2008, 04:51 AM
#1
Lists STL
small question about LIST stl...
My code looks something like this...
Code:
#include <list>
#include <iostream>
using namespace std;
void main()
{
list<int> randomNums;
int number20 = 0;
while( number20 < 5 )
{
(number20++);
randomNums.push_back((2*number20) + 1);
randomNums.push_back(2*number20);
}
list<int>::iterator iter = randomNums.begin();
randomNums.erase(randomNums.end());
for( list<int>::iterator iter = randomNums.begin() ; iter != randomNums.end() ; iter++)
{
cout << (*iter) << endl;
}
}
And i get en error when trying to use the erase command with the randomNums.end(); which makes sense.
>>>HOW CAN I ERASE THE LAST ELEMENT FROM THE LIST?
also when I try to erase using this code:
[code]
randomNums.erase(iter++);
[/coode]
it won't erase the second entry.
>>WHY DOES LIST FUNCTION LIKE THIS??
-
April 1st, 2008, 05:16 AM
#2
Re: Lists STL
randomNums.end() returns the iterator to one past the last element in the list.
If you want to erase the last element then you you need to subtract 1 from the end() iterator or use rbegin() to get the iterator.
Also randomNums.erase(iter++) increments the iterator after setting the parameter for the function.
Last edited by JohnW@Wessex; April 1st, 2008 at 05:20 AM.
-
April 1st, 2008, 05:32 AM
#3
Re: Lists STL
I see --- so "--iter" would fix the problem!!!
also I was reading this book and I noticed this example which I've never seen it before... what does it signify?
Code:
list<char> list1 = make< list<char> >("remembering");
whats the make for? In my last example I didn;t use it.
-
April 1st, 2008, 05:43 AM
#4
Re: Lists STL
If you want to erase the last element then you you need to subtract 1 from the end() iterator or use rbegin() to get the iterator.
list also has pop_back(), so it makes sense to use it instead of erase() for the last element.
also I was reading this book and I noticed this example which I've never seen it before... what does it signify?
I do not think make() is in the C++ standard library, so what it does depends on the implementation.
-
April 1st, 2008, 03:11 PM
#5
Re: Lists STL
Well if your specifically want to delete the last element yes, but if it's a general loop that deletes lets say blocks every time a ball hits them, then u need to use erase.
Thx all.
-
April 1st, 2008, 09:00 PM
#6
Re: Lists STL
Originally Posted by armen_shlang
Well if your specifically want to delete the last element yes, but if it's a general loop that deletes lets say blocks every time a ball hits them, then u need to use erase.
Thx all.
Hello there,
you didn't quite understand what laserlight meant.
in the example you gave us,
1) your approach to delete the element in the list is encumbered in comparison with what laserlight suggested.
2) you have to be very careful when you use an iterator to delete an element - especially in a loop you're thinking about - thus, your approach is more error-prone.
hope this helps.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|