samedi 25 juin 2016

complexity of minmax_element

How is minmax_element typically implemented? I can see the time complexity is at most max(floor(3/2(N−1)), 0) applications of the predicate, where N = std::distance(first, last).

Where minmax_element allows me to find the smallest and largest elements in a range of elements that can be iterated over (read: containers). For example:

#include <algorithm>
#include <vector>
using namespace std;

void Algorithm_minmax_element()
{
    double x = 2, y = 1, z = 0;
    vector<double> v = { 2, 1, 0, -1 };

    auto result1 = minmax(x, y);
    // result1 == pair(1, 2)
    auto result2 = minmax({ x, y, z });
    // result2 == pair(0, 2)
    auto result3 = minmax_element(v.begin(), v.end());
    // result3 == pair(&v[3] = -1, &v[0] = 2)
}

Aucun commentaire:

Enregistrer un commentaire