Getting maximum values in array There is a function which is getting maximum values of each period-length interval in array.
I am trying to optimize this function by performance.  

void maximumsT(const std::vector&lt;double&gt; &amp;src, size_t period, std::vector&lt;double&gt; &amp;dst)
{
    dst.resize(src.size());

    for (size_t i = 1; i &lt;= period; ++i) {
        dst[i - 1] = *std::max_element(src.begin(), src.begin() + i);
    }

    for (size_t i = period; i &lt;= src.size(); ++i) {
        dst[i - 1] = *std::max_element(src.begin() + i - period, src.begin() + i);
    }
}


I asked this SO question
and got this solution.  

Then I tried to implement the solution.

#include &lt;vector&gt;
#include &lt;deque&gt;
#include &lt;algorithm&gt;

void maximumsT(const std::vector&lt;double&gt; &amp;src, size_t period, std::vector&lt;double&gt; &amp;dst)
{
    dst.resize(src.size());

    for (size_t i = 1; i &lt;= period; ++i) {
        dst[i - 1] = *std::max_element(src.begin(), src.begin() + i);
    }

    for (size_t i = period; i &lt;= src.size(); ++i) {
        dst[i - 1] = *std::max_element(src.begin() + i - period, src.begin() + i);
    }
}

void maximums(const std::vector&lt;double&gt; &amp;src, size_t period, std::vector&lt;double&gt; &amp;dst)
{
    dst.resize(src.size());
    std::deque&lt;std::pair&lt;size_t, double&gt;&gt; deq;

    for (size_t i = 0; i &lt; src.size(); ++i) {
        if (!deq.empty() &amp;&amp; i &gt;= period &amp;&amp; deq.front().first &lt;= i - period) {
            deq.pop_front();
        }

        while (!deq.empty() &amp;&amp; deq.back().second &lt; src[i]) {
            deq.pop_back();
        }

        deq.push_back(std::make_pair(i, src[i]));
        dst[i] = deq.front().second;
    }
}

bool test()
{
    std::vector&lt;double&gt; v(1 + rand() % 1000);
    for (double &amp;e : v) {
        e = (double)rand() / RAND_MAX;
    }

    size_t period = 1 + (rand() % v.size());
    std::vector&lt;double&gt; vv, vvv;

    maximumsT(v, period, vv);
    maximums (v, period, vvv);

    return vv == vvv;
}

bool test(size_t iterations)
{
    for (size_t i = 0; i &lt; iterations; ++i) {
        if (!test()) {
            return false;
        }
    }

    return true;
}

int main(int argc, char *argv[])
{
    return test(10000);
}


How can I improve and optimize this code?
The code in maximums is doing what it is supposed to do, based on that SO question. I have several remarks about the other parts though.

Do not use single letter variables. Use names that say something.
So no v, vv, vvv. 

Another naming issue, your two maximums functions do the same thing but with a different approach, you can make this clear in the name. They both use T=period, so this is not distinguishing. Maybe use the fact that a deque is used or that one is the simple variant.

You create double vectors in the test and then pass them to maximums for example. There is absolutely no reason for test to create them. Let maximums create the vector, since it needs it and then return it. Some for maximumsT.

Your maximumsT is not intuitive. There is no need to do a double loop.
Why did you change your implementation as in your question on SO? It was better there. 
Namely

for (size_t i = period; i &lt;= v.size(); ++i) {
        vv[i - 1] = *std::max_element(v.begin() + i - period, v.begin() + i);
    }


The code will not be faster or save space, but its shorter and clearer. 

Initializing random vectors to test against, could be done in a dedicated function. I would make the test also parameterized on the source vector.

My suggestion:

#include &lt;vector&gt;
#include &lt;deque&gt;
#include &lt;algorithm&gt;

std::vector&lt;double&gt; maximumsT(const std::vector&lt;double&gt; &amp;src, size_t period)
{
    std::vector&lt;double&gt; dst(src.size());

    for (size_t i = period; i &lt;= v.size(); ++i) {
       vv[i - 1] = *std::max_element(v.begin() + i - period, v.begin() + i);
    }
    return dst;
}

std::vector&lt;double&gt; maximums(const std::vector&lt;double&gt; &amp;src, size_t period)
{
    std::vector&lt;double&gt; dst(src.size());
    std::deque&lt;std::pair&lt;size_t, double&gt;&gt; deq;

    for (size_t i = 0; i &lt; src.size(); ++i) {
        if (!deq.empty() &amp;&amp; i &gt;= period &amp;&amp; deq.front().first &lt;= i - period) {
            deq.pop_front();
        }

        while (!deq.empty() &amp;&amp; deq.back().second &lt; src[i]) {
            deq.pop_back();
        }

        deq.push_back(std::make_pair(i, src[i]));
        dst[i] = deq.front().second;
    }
    return dst;
}

std::vector&lt;double&gt; randomVect()
{
    std::vector&lt;double&gt; v(1 + rand() % 1000);
    for (double &amp;e : v) {
        e = (double)rand() / RAND_MAX;
    } 
    return v
}
bool compareMaximums(std::vector&lt;double&gt; v, size_t period)
{
    return maximumsT(v, period) == maximums(v, period);
}

bool test(size_t iterations)
{
    for (size_t i = 0; i &lt; iterations; ++i) {
        std::vector&lt;double&gt; srcVec = randomVect();
        size_t period = 1 + (rand() % srcVec.size());
        if (!compareMaximums(srcVec, period)) {
            return false;
        }
    }

    return true;
}

int main(int argc, char *argv[])
{
    return test(10000);
}