CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: List Class

  1. #1
    Join Date
    May 2006
    Posts
    19

    List Class

    Dears

    I created a list class as

    list<string> mylist

    then added data
    mylist.push_back("A");
    mylist.push_back("B");
    mylist.push_back("C-VIP");
    mylist.push_back("D-VIP");


    Now, I would like to create another list for keeping track of the
    address of certain entries in the mylist class such as:

    list<whatever??> myptr;

    I would like myptr to have the addresses of the VIP entries above. Is
    that possible?

    How?

    Thank You

    HAOB BOY

  2. #2
    Join Date
    Dec 2004
    Location
    Paso de Robles
    Posts
    296

    Re: List Class

    The list class is designed to keep you away from raw access to pointers (That's why iterators were invented). The other problem you face is that when a list is updated the iterators become invalidated, so you can't store them easily either. Perhaps there is a way around this limitation. What is the purpose of keeping track of the addresses (pointers?) of the VIP entries?

  3. #3
    Join Date
    May 2006
    Posts
    19

    Re: List Class

    may be the VIP example is not good but the purpose I want to keep track of certain nodes in a Linked List is that I am creating a memory-based database for a school project. There are several linked lists inmy project: one of them contains the table definitions which I will refer to whenever I need to add a record to the table linked list.

    My probelm is how to know that a certain node is a definistion for certain table.

    Thanx

  4. #4
    Join Date
    Dec 2004
    Location
    Paso de Robles
    Posts
    296

    Re: List Class

    Have you thought of using sets or multisets?

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: List Class

    Quote Originally Posted by binarybob0001
    The other problem you face is that when a list is updated the iterators become invalidated,
    This is not necessarily true for lists. The iterator to an element remains valid for the lifetime of the list, unless the element is removed from the list.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: List Class

    Quote Originally Posted by HAOBBOY
    may be the VIP example is not good but the purpose I want to keep track of certain nodes in a Linked List
    Then you need to keep track of the iterator to an element in the list, not pointer to an element in the list.

    Basically when dealing with std::list, you cannot use pointers to address elements, only iterators. Yes, a list::iterator may be implemented as a pointer, but iterators and pointers are not the same thing.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    May 2006
    Posts
    19

    Re: List Class

    Thank you all

    can I do this:

    string d;
    d="mylist";
    list<string> d.c_str();

    I would like to be able to create linked list at run time?

  8. #8
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: List Class

    Quote Originally Posted by HAOBBOY
    Thank you all

    can I do this:

    string d;
    d="mylist";
    list<string> d.c_str();

    I would like to be able to create linked list at run time?
    Your linked list would be created at runtime. But why do you want to name it at runtime?

  9. #9
    Join Date
    May 2006
    Posts
    19

    Re: List Class

    Because, I am reading a file that has names and I need to make a linked list for each name.

  10. #10
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: List Class

    Have a map with the following specifications:
    Code:
    std::map<std::string, std::list<whatever> > myMapOfLists;
    This will help you achieve what you want. Regards.

  11. #11
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: List Class

    Quote Originally Posted by HAOBBOY

    Because, I am reading a file that has names and I need to make a linked list for each name.
    In that case, you can use a std::map<std::string, std::list<std::string> >
    The key of the map, would be the "name", and the value, would be the list.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  12. #12
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: List Class

    Quote Originally Posted by Paul McKenzie
    Then you need to keep track of the iterator to an element in the list, not pointer to an element in the list.

    Basically when dealing with std::list, you cannot use pointers to address elements, only iterators. Yes, a list::iterator may be implemented as a pointer, but iterators and pointers are not the same thing.
    Even when adding or removing elements in a list, references (and pointers) to other objects are not at all invalidated.

    For example, lib.list.modifiers says that
    iterator erase(iterator position);
    iterator erase(iterator first, iterator last);

    void pop_front();
    void pop_back();
    void clear();.

    Effects:
    Invalidates only the iterators and references to the erased ele-
    ments.
    Throws:
    Nothing.
    And, having pointers to items of any container (except std::vector<bool> which does not met container requirements), it is perfectly valid to have references to items inside the container, as long as no modifying functions are called.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  13. #13
    Join Date
    May 2006
    Posts
    19

    Re: List Class

    Can I do nested maps

    Code:
    std::map<std::string, std::map<std::string,std::string> > myMapOfLists;
    the above gives me error

  14. #14
    Join Date
    May 2006
    Posts
    19

    Re: List Class

    Guys/Gals,

    I managed to to have nested map, but how can I add data to the 2nd map then add this to the mother map?

    Also, how can I reference the elements inside these maps?

  15. #15
    Join Date
    May 2006
    Posts
    19

    Re: List Class

    How can I declare an iterator for the nested map?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured