Improving your templates' skills

Pages: 12
New example of queue, more complex. Without use of std vector

http://surprising-code.blogspot.com/p/example8.html
That implementation is quite inefficient. Erasing from the front of the vector is O(n).
You could have used a list there (and you have O(1) for insertion and deletion).

Another way for O(1) will be using the vector as it was circular. It will be keeping index to the front and the back.
However you cannot destroy the objects*, when popping you just update the index.

*Actually there is a way, but you've got to sacrifice the container. You'll use an allocator instead http://cplusplus.com/reference/std/memory/allocator/
The allocator will use placement new to construct the objects, and will call at the destructor to destroy them
You are right, a list would be a better choise but in fact I don't care. I don't know if you have read all blog but this is the first example of using templates in a class I did so, I just wanted to do it easy.

http://surprising-code.blogspot.com/p/example8.html
This is a better example.

About allocator, I'm trying to go slowly, but I will teach it sometime.

Anyway, thank you very much for your advice ^ ^
I'll update my notes to people note the efficient of.
Topic archived. No new replies allowed.
Pages: 12