I was also wondering how to write a peek function for a queue, that will return the string at the front of my queue, but not remove it?
The text above is a follow-up to ...
i was wondering how to derive a class from a vector. any suggestions?
Yes. In short donít.
The C++ standard library containers are _not_ designed to be derived from. Doing so is very poor style.
For a start they have non virtual public destructors. If a pointer to a std::vector class template specialisation were used to delete an instance of your super_vector derivation (or super_vector specialisation if it is a class template) your super_vector destructor will _not_ be called. I use the term specialisation to refer to an actual class formed from a class template such as std::vector by providing the template with template arguments.
That is a specific specialisation of std::vector such as std::vector<int> is a concrete class.
Instead use composition (a.k.a. containment, layering or embedding): develop classes that have std::vector specialisations as data members:
You could also develop class templates that pass some or all of their template arguments on to their std::vector data members:
template <typename T, class Allocator=std::allocator<T> >
std::vector<T, Allocator> values_;
Note that I have provided the G class template above with the _same_ template parameters as std::vector, with the same default value for the Allocator parameter.
However if you really, really, really have to derive from std::vector (i.e. you have _no_ other choice or you really know what you are doing) you can of course do so, but on your own head be it:
class AD : public std::vector<int>
template <typename T>
class GD : public std::vector<T>
Assuming you are using the C++ standard library, which is likely judging from your previous question, why bother? The standard C++ container adapter std::queue functions in this fashion anyway. The operation is called front() Ė it returns the next element in the queue without removing it. To remove an element from the queue you use the pop() operation. To add an element to the queue you use push().
Exactly how to write such a peek function would depend on the implementation of your queue, but the pseudo code would look something like:
string_queue::peek() : returns reference to string
If the queue is empty
Then throw EmptyQueue exception
Return the next queue item
In reality you would probably need to have two such functions. The second would be a const member function returning a reference to const string.
Returning the next queue item does not cause it to be removed. Adding another item to the queue adds it to the other end of the queue meaning that calling peek before and after items are added to the queue will reference the same queue item.
For example if your queue were based on a doubly linked list of some sort then the next queue item would be the value held by the last (or back) list node and new queue items would be added to the front list node.
To obtain the last item without removing it you would use the back-link of the list (itís doubly linked remember) to access the last node and return a reference to that nodeís data item.
To remove an item from the queue (e.g. via a separate pop operation), without accessing its value, you would simply delete the last list node.
Note that by default a std::queue uses a std::deque (pronounced "deck") as its wrapped container type. A deque is a structure for which it is efficient to insert and delete elements at _both_ ends of the collection. This differs from a vector for which it is only efficient to insert and delete items from the end (back) of the collection.
You might like to examine the implementation of the std::queue implementation for the standard C++ library implementation you are using. You should start with the queue header. You may have to follow the #include directives and examine other headers as the details may be in some other file included directly or indirectly by queue. You will probably see something like this:
template < class T, class ContainerT = deque<T> >
const_reference front() const
Where container_ is a member of the type of the wrapped container (ContainerT template parameter) used to implement the queue (std::deque<T> by default) and reference and const_reference are typedefs (type aliases) for reference to the queued type as non-constant and constant.
As you can see this really is a wrapper class (or rather class template). All it does is hand the work off to the container member, including handling accesses to an empty container. You will note that std::queue uses the composition style recommended in the answer to your previous question.