A simple C++ Deque example

Being a polymath and a programming polyglot person can be really exhausting, let alone intimidating at some point. Therefore I have decided to write a few things down that would be used as a backup and as a reminder to this rusty brain.

Today we will see a very simple example how double-end queues work, also known as deque.

So, what is a double-end queue? It's a queue container that would let you add or remove elements on both of its sides; bear in mind that this happens dynamically behind the scene for you, therefore you need no worry about it.

Time for our example:

#include <iostream>
#include <deque>

int main()
{
    std::deque<int> val;

    for (int i = 0; i < 10; ++i)
    {
        val.push_front(i);
        std::cout << "front value: " << val.front() << '\t';
        val.push_back( (i * i) + 9 );
        std::cout << "back value: " << val.back() << '\n';
    }
    std::cout << '\n';

    for (auto i = val.begin(); i != val.end(); ++i)
    {
        std::cout << "pop front: " << val.front() << '\t';
        val.pop_front();
        std::cout << "pop back: " << val.back() << '\n';
        val.pop_back();
    }
}

OK, what does it do?

Basically we create a deque that accepts values of type integer and in our first loop, we push i on the front side of queue and (i * i) + 9 on the back of it.

On the second loop, we use auto as our variable initializer to assign val's beginning value to i and while has not reached the end of it, to first print the value that's about to get popped from the front side and then from the back.

Well, that was it; pretty straight forward I would say.

Alright, that's it for today.

If anyone has a question, please don't hesitate to ask.

Cheers.