Thread: using std::list<>::iterator?

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    using std::list<>::iterator?

    I had these statements in my prog:
    Code:
    #include <iostream>
    #include <fstream>
    #include <cmath>
    #include <list>
    using std::list;
    using std::ofstream;
    using std::cout;
    using std::cin;
    and when I tried to advance my iters, I got infinite loops.

    Do I have to say something like:
    Code:
    using std::list<>::iterator;
    ????????????

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Your infinite loops probably have absolutely nothing to do with "using" statements.

  3. #3
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    I should add that if I just said:

    using namespace std;


    everything works fine.



    it all seems unrelated to me but maybe you C++ gods know better

  4. #4
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    yeah that's what i figured. i guess it wouldn't have let me even declare an iterator if that was the case. anyway, i just had to hold down ctrl-z until it backed my prog up to where it was working okay. (how's that for debugging a program, haha!)

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What does your loop look like?

  6. #6
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    I basically had a function like this:
    Code:
    list<Coordinates>::iterator partition_segment(list<Coordinates>& mList, list<Coordinates>::iterator it){
    
        list<Coordinates> tempList;
    
        double xPos = it->get_x();
    
        ++it;
    
        double nextXPos = it->get_x();
    
        // Do stuff with my data here
    
        // Populate my temp list:
        tempList.push_back(aCoordinate);
        tempList.push_back(aCoordinate2);
        tempList.push_back(aCoordinate3);
    
        mList.splice(it, tempList);
    
        return it;
    }
    This function was called in main like so:
    Code:
    for(int i = 0; i < numLoops; i++){
    
        list<Coordinate>::iterator pos = aList.begin();
    
        while(pos != aList.end()){
    
            pos = partition_segment(aList, pos);
        }
    }
    it works fine now, and I undid so many things that I won't know where the problem was now.

  7. #7
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    What does splice do? If it modifies mList (it appears that it might) then it is a mistake, because it would (possibly) invalidate your iterator. The basic rule is if you modify a container any iterator pointing into that container is not guaranteed to be valid afterwords.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The basic rule is if you modify a container any iterator pointing into that container is not guaranteed to be valid afterwords.
    This does not apply to std::list: inserting and deleting elements from a std::list does not invalidate pointers, references and iterators to other elements of that std::list.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by IfYouSaySo View Post
    What does splice do? If it modifies mList (it appears that it might) then it is a mistake, because it would (possibly) invalidate your iterator. The basic rule is if you modify a container any iterator pointing into that container is not guaranteed to be valid afterwords.
    http://www.cppreference.com/cpplist/splice.html

    It inserts one list into another list at the specified position.
    I don't believe it invalidates any iterators since this is a list, not a vector.

Popular pages Recent additions subscribe to a feed