C++11标准模板(STL)- 算法库 - 最小/最大操作 - 在一对边界值间夹逼一个值(std::clamp)

算法库 

算法库提供大量用途的函数(例如查找、排序、计数、操作),它们在元素范围上操作。注意范围定义为 [first, last) ,其中 last 指代要查询或修改的最后元素的后一个元素。

在一对边界值间夹逼一个值

std::clamp

template<class T>
constexpr const T& clamp( const T& v, const T& lo, const T& hi );

(1)(C++17 起)

template<class T, class Compare>
constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp );

(2)(C++17 起)

1) 若 v 小于 lo ,则返回 lo ;若 v 大于 hi ,则返回 hi ;否则返回 v 。使用 operator< 比较值。

2) 同 (1) ,不过使用 comp 比较值。

lo 的值大于 hi 则行为未定义。

参数

v-要夹住的值
lo,hi-v 的边界
comp-比较函数对象(即满足比较 (Compare) 要求的对象),若 lo 小于 vv 小于 hi,则返回 ​true 。

比较函数的签名应等价于如下:

 bool cmp(const Type1 &a, const Type2 &b);

虽然签名不必有 const & ,函数也不能修改传递给它的对象,而且必须接受(可为 const 的)类型 Type1Type2 的值,无关乎值类别(从而不允许 Type1 & ,亦不允许 Type1 ,除非 Type1 的移动等价于复制 (C++11 起))。
类型 Type1 与 Type2 必须使得 T 类型的对象能隐式转换到这两个类型。 ​

类型要求
- 为使用重载 (1) , T 必须满足可小于比较 (LessThanComparable) 的要求。然而若避免了 NaN ,则 T 能为浮点类型。

返回值

v 小于 lo 到则为 lo 的引用,若 hi 小于 v 则为到 hi 的引用,否则为到 v 的引用。

复杂度

1) 至多二次比较

可能的实现

版本一
template<class T>
constexpr const T& clamp( const T& v, const T& lo, const T& hi )
{
    assert( !(hi < lo) );
    return v < lo ? lo : hi < v ? hi : v;
}
版本二
template<class T, class Compare>
constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp )
{
    assert( !comp(hi, lo) );
    return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
}

调用示例

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <time.h>
#include <random>
#include <vector>
#include <cassert>

struct Cell
{
    int x;
    int y;

    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}

    Cell &operator +=(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator +(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator *(const Cell &cell)
    {
        x *= cell.x;
        y *= cell.y;
        return *this;
    }

    Cell &operator ++()
    {
        x += 1;
        y += 1;
        return *this;
    }


    bool operator <(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y < cell.y;
        }
        else
        {
            return x < cell.x;
        }
    }

    bool operator >(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y > cell.y;
        }
        else
        {
            return x > cell.x;
        }
    }

    bool operator ==(const Cell &cell) const
    {
        return x == cell.x && y == cell.y;
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

namespace std
{
template<class T>
const T& clamp(const T& v, const T& lo, const T& hi)
{
    assert(!(hi < lo));
    if (v < lo)
    {
        return lo;
    }
    else if (hi < v)
    {
        return hi;
    }
    else
    {
        return v;
    }
}

template<class T, class Compare>
const T& clamp(const T& v, const T& lo, const T& hi, Compare comp)
{
    assert(!comp(hi, lo));
    return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
}
}

int main()
{
    std::cout << std::boolalpha;

    std::mt19937 g{std::random_device{}()};
    srand((unsigned)time(NULL));

    auto generate = []()
    {
        int n = std::rand() % 10 + 110;
        Cell cell{n, n};
        return cell;
    };

    //3) 构造拥有 count 个有值 value 的元素的容器。
    std::vector<Cell> vector1(8, generate());
    std::generate(vector1.begin(), vector1.end(), generate);
    std::sort(vector1.begin(), vector1.end());
    std::cout << "vector1:  ";
    std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::vector<Cell> vector2(vector1.size(), generate());
    std::generate(vector2.begin(), vector2.end(), generate);
    std::sort(vector2.begin(), vector2.end());
    std::cout << "vector2:  ";
    std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    Cell cell = generate();
    for (size_t index = 0; index < vector1.size(); ++index)
    {
        std::cout << "std::clamp(" << cell << "," << vector1.at(index)
                  << "," << vector2.at(index) << "):   ";
        //1) 若 v 小于 lo ,则返回 lo ;若 v 大于 hi ,则返回 hi ;否则返回 v 。使用 operator< 比较值。
        std::cout << std::clamp(cell, vector1.at(index), vector2.at(index)) << std::endl;
    }

    std::cout << std::endl;

    auto Compare = [](const Cell & cellA, const Cell & cellB)
    {
        return cellA < cellB;
    };

    for (size_t index = 0; index < vector1.size(); ++index)
    {
        std::cout << "std::clamp(" << cell << "," << vector1.at(index)
                  << "," << vector2.at(index) << "):   ";
        //2) 同 (1) ,不过使用 comp 比较值。
        std::cout << std::clamp(cell, vector1.at(index), vector2.at(index), Compare) << std::endl;
    }

    return 0;
}

输出

vector1:  {110,110} {111,111} {111,111} {112,112} {113,113} {113,113} {115,115} {117,117}
vector2:  {110,110} {111,111} {112,112} {113,113} {113,113} {113,113} {115,115} {119,119}
std::clamp({117,117},{110,110},{110,110}):   {110,110}
std::clamp({117,117},{111,111},{111,111}):   {111,111}
std::clamp({117,117},{111,111},{112,112}):   {112,112}
std::clamp({117,117},{112,112},{113,113}):   {113,113}
std::clamp({117,117},{113,113},{113,113}):   {113,113}
std::clamp({117,117},{113,113},{113,113}):   {113,113}
std::clamp({117,117},{115,115},{115,115}):   {115,115}
std::clamp({117,117},{117,117},{119,119}):   {117,117}

std::clamp({117,117},{110,110},{110,110}):   {110,110}
std::clamp({117,117},{111,111},{111,111}):   {111,111}
std::clamp({117,117},{111,111},{112,112}):   {112,112}
std::clamp({117,117},{112,112},{113,113}):   {113,113}
std::clamp({117,117},{113,113},{113,113}):   {113,113}
std::clamp({117,117},{113,113},{113,113}):   {113,113}
std::clamp({117,117},{115,115},{115,115}):   {115,115}
std::clamp({117,117},{117,117},{119,119}):   {117,117}

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值