Custom container should expand STL containers?

This is a discussion on Custom container should expand STL containers? within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, For a personal project i'm working on i need to have a custom container which is supposed to ...

  1. #1
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89

    Custom container should expand STL containers?

    Hi everyone,

    For a personal project i'm working on i need to have a custom container which is supposed to work halfway between a std::list and a lifo stack. According to my calculations i would need the following methods:
    • no random access
    • ++ and -- operations for traversing the existing nodes
    • pop() - pops the current node
    • pop_back() - pops the last node
    • push_back() - adds a node to the end of the container
    • value() - reads the current node
    Now i was thinking about taking a std::list and expanding it by encapsulating a std::list object in my own class, but i'm not really sure if that would be the best approach, what do you guys think?

    Thanks for your time and help in advance, Cheers.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> i was thinking about taking a std::list and expanding it by encapsulating a std::list object in my own class
    Sounds good to me.

    >> pop() - pops the current node
    You mean erase? pop() makes me think of popping off the end.

    >> i need to have a custom container
    You should prefer a built-in solution if one exists unless you are doing it for the learning experience. You may be able to customize std::stack to use a list for the underlying container.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    After further research...

    Since the standard stack class uses pop() to pop the top of the stack only, it is confusing to change that behavior and use it for erasing a non-top element, so I am even more against the use of that name for that function.

    Using std::stack<T, std::list<T> > would work, but would not provide the ability to get the value of or erase a non-top element since std::stack doesn't provide iterators.

    If you can, check out Josuttis' The C++ Standard Library. Chapter 10 is about the container adapters like stack, and includes his own implementation that would be a good start for what you want to do. I'm not sure how easily you'll be able to add the extra list-like stuff.

    Is there any reason you don't just use std::list?

  4. #4
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    Thx for your replies Daved,

    I guess there's no reason for not using std::list directly, i was trying to encapsulate the behavior in a more limited interface but i guess there's no real point in doing that, after all this won't be a public interface for other developers anyway. You're right, i'll be using std::list for this.

    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. std::map question.
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 03-19-2008, 12:29 PM
  2. sorting container
    By l2u in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2007, 01:12 PM
  3. stl container to store more than 1 type
    By sujeet1 in forum C++ Programming
    Replies: 7
    Last Post: 05-09-2007, 04:10 AM
  4. stl containers allocation in heap or stack?
    By sawer in forum C++ Programming
    Replies: 9
    Last Post: 08-06-2006, 03:08 PM
  5. Linked List Queue Implementation help
    By Kenogu Labz in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2005, 10:14 AM

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