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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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.

  3. #3
    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