Implementing STL list::insert

This is a discussion on Implementing STL list::insert within the C++ Programming forums, part of the General Programming Boards category; I'm trying to design an insert() like the list::insert: Code: iter1 = mlist.insert(iter2, myData); My problem is I can't convert ...

  1. #1
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127

    Implementing STL list::insert

    I'm trying to design an insert() like the list::insert:
    Code:
    iter1 = mlist.insert(iter2, myData);
    My problem is I can't convert from an Iterator to a Node.
    Code:
    iterator insert(iterator i, const T& d){
    
        /*  can't do this: */
       Node *c = i.curr;
    }
    and at least right now I can't figure out another way to convert w/out iterating the list (again) which seems silly since the function takes an iterator as an argument.

    Any thoughts?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Make it a friend to the iterator class, and you can use the private members of the class.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    Is that make iterator a friend class of list?

  4. #4
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    Nevermind, I got it straightened out.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sh3rpa View Post
    Nevermind, I got it straightened out.
    I take it you made it a friend of your iterator class?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    si, muchos gracias

    (my spanish is about as good as my french!)

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sh3rpa View Post
    si, muchos gracias

    (my spanish is about as good as my french!)
    You are welcome - I speak no french or spanish either. [Aside from the few standard phrases that'll get me some food or drink when the staff refuse/can't speak English - and I have to hope that they are wanting to sell me some such, otherwise god knows what I'd end up with]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    I'm having trouble w/writing the definition of functions that return an Iter

    Code:
    template <typename T> Iter
    List<T>::insert()
    
    /* etc */
    }
    ^
    doesn't work

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Iter is a nested class? Then it should look something like this:
    Code:
    template <typename T>
    typename List<T>::Iter List<T>::insert() {}
             //return type  class   method
    The second typename is (may be) required to tell the compiler that T is a template parameter.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    Quote Originally Posted by anon
    Iter is a nested class? Then it should look something like this:
    Code:
     	Code:
     	template <typename T>
    typename List<T>::Iter List<T>::insert() {}
             //return type  class   method
    The second typename is (may be) required to tell the compiler that T is a template parameter.
    worked like a charm! thanx!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Formatting Using STL
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2004, 04:52 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. STL or no STL
    By codec in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2004, 02:36 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

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