dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Lists STL

Hybrid View

  1. #1
    Join Date
    Jan 2008
    Posts
    60

    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??

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    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.

  3. #3
    Join Date
    Jan 2008
    Posts
    60

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jan 2008
    Posts
    60

    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.

  6. #6
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Lists STL

    Quote 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)