using std::list<>::iterator?

This is a discussion on using std::list<>::iterator? within the C++ Programming forums, part of the General Programming Boards category; I had these statements in my prog: Code: #include <iostream> #include <fstream> #include <cmath> #include <list> using std::list; using std::ofstream; ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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
    22,312
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21