STL linked lists

This is a discussion on STL linked lists within the C++ Programming forums, part of the General Programming Boards category; Hi all, need to ask quick question about stl linked lists. When i push objects into a list am i ...

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    STL linked lists

    Hi all,
    need to ask quick question about stl linked lists.

    When i push objects into a list am i correct in saying that on each push
    "new" objects are created i.e. the linker fixes addresses for those objects in the container.
    While the address for those objects in the container are different the value they hold are "copied" from the object that was pushed in.

    So if i have
    Code:
    class myClass
    {
        int a ; 
       myClass()
      {
           a =10 ;
    
       }
    }
    myClass a ;
    list <myClass> ; 
    
    myClass.push_back(a) ;
    the member variable a for the new objects in the list would have the value 10 unless changed.
    So is my logic flawed?

    Sorry for this ridiculous question. I had to use linked lists all of a sudden, so have to read it on my own.
    The main question though is the erase function using an iterator.
    Code:
    while(it!=myClass.end())
    {
        if(....)
        {
    
                    it=  myClass.erase() ; 
              }
    }
    why do we need to initialize it to myClass.erase()
    what does erase() return?
    would not do if we just increment it?
    so
    Code:
          myClass.erase() 
          it++ ;
    in a hurry, sorry if formatting is bad.
    Thanks in advance

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    When you push an object into a list, the object is copied. That is, a new object is created inside the list and the old value is copied over. Otherwise it wouldn't take a reference.
    About erase... consider the list (below) and an iterator to the "5".

    Code:
    [ 1 2 3 4 5 6 7 8 9 ]
              ^
    If we now "erase" that 5, where does the iterator point? If we're lucky, it might point to 6. But then again, it might not.
    Why not? Because there is no pointer "next to" 5 in memory. All the items are linked together with pointers to different places in memory.
    So to ensure we have a valid iterator, we must reassign it after calling erase.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Elysia View Post
    When you push an object into a list, the object is copied. That is, a new object is created inside the list and the old value is copied over. Otherwise it wouldn't take a reference.
    About erase... consider the list (below) and an iterator to the "5".

    Code:
    [ 1 2 3 4 5 6 7 8 9 ]
              ^
    If we now "erase" that 5, where does the iterator point? If we're lucky, it might point to 6. But then again, it might not.
    Why not? Because there is no pointer "next to" 5 in memory. All the items are linked together with pointers to different places in memory.
    So to ensure we have a valid iterator, we must reassign it after calling erase.
    Oh right, excellent :P
    i assumed stl linked lists were structured sequentially in memory.
    your explanation makes sense, ty much.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Quote Originally Posted by Eman View Post
    i assumed stl linked lists were structured sequentially in memory.
    That's a vector.
    But the same principle applies to vectors, as well. If we delete 5, then the iterator will point to 6. So if you then increment it, it will point to 7, which you'll delete instead.
    Take care.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Elysia View Post
    That's a vector.
    But the same principle applies to vectors, as well. If we delete 5, then the iterator will point to 6. So if you then increment it, it will point to 7, which you'll delete instead.
    Take care.
    em I haven't done vectors yet. In fact we didn't even cover linked lists just found it was better to use it.

    but if the same concept apply to vectors, but i think i can see why it delete 7.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Ah, well, I don't suppose it is that important to understand them now, but later you are definitely going to need to learn what different containers are and do.
    Each has their advantages and weaknesses.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    yeah true, i assume it is much more difficult to implement linked lists in C .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists?!
    By hwing91 in forum C Programming
    Replies: 32
    Last Post: 11-08-2010, 05:13 PM
  2. STL Linked Lists
    By stanlvw in forum C++ Programming
    Replies: 12
    Last Post: 04-11-2008, 01:33 AM
  3. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM

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