• Advertisement

Archived

This topic is now archived and is closed to further replies.

STL question

This topic is 5500 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So STL is highly-optimized, right? And what if i use my own reducted queue template, which does not provide full functionality as in STL, but provides simple and easy interface to queues. Would it be faster than STL queues?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
That depends. For instance, you could customize which tradeoffs you would like to make. For instance, you could choose not to keep a counter of the number of items in the queue. This would make it work slightly faster when pushing or popping the queue. But it would make it slower when trying to count the number of items in the queue.

The odds of you creating a queue that is unconditionally faster than the STL queue is extremely slim unless you impose some pretty strict limitations.

On the other hand, if you want to know more about queues, there is no better way to learn than to implement it yourself.

What I would do is use a typedef somewhere like ''typedef queue<int> MyIntQueue'' or something and then replace the typedef with a class later if you find that you absolutely need to customize some behavior.

-D

Share this post


Link to post
Share on other sites
Is there a way to pop() and front() a queue with a single function? for ex. a method pop_front() which returns the front queue entry...

Share this post


Link to post
Share on other sites
Sure there is:

  

template <
class Type,
class Container = deque<Type>
>
class my_qeue : public queue<Type, Container>
{
public:
const Type pop_front()
{
Type result = front();
pop();
return result;
}
};


Share this post


Link to post
Share on other sites
No built-in methods? that''s strange. Wrapping around a queue utilizes a lot of resources.

Share this post


Link to post
Share on other sites
It makes it more exception safe to have querying the queue and popping the value as seperate operations. If pop returned the value and the assignment operator of the class threw an exception you would lose the value.

What's that rule about 'One Responsibility'?

darookie: In your example you don't check whether the queue is empty. If you call front() or pop() on an empty queue it will throw an exception. Is that intentional? I guess you're assuming it is checked before being called.

It is confusing to name a function pop_front as the standard deque has pop_front which doesn't return a value. Very confusing.

[edited by - petewood on February 3, 2003 8:16:18 AM]

Share this post


Link to post
Share on other sites
Don''t get hung up about functionality you''re not going to use. These are templates. If you don''t call it, there''s no code for it. Also, they''re inlined. so chances are getting the front and popping separately would provide almost the same code as doing both with one operation.

As for "Wrapping around a queue utilizes a lot of resources", if you don''t add any more data, what extra resources are being utilised?


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
quote:
Original post by petewood
darookie: In your example you don''t check whether the queue is empty. If you call front() or pop() on an empty queue it will throw an exception. Is that intentional? I guess you''re assuming it is checked before being called.


Now that you mention it - since pop() and front() throw an exception on empty queues (btw, are you sure about that?), then
it''s natural for pop_front() to behave the way.

Share this post


Link to post
Share on other sites

  • Advertisement