算法库
算法库提供大量用途的函数(例如查找、排序、计数、操作),它们在元素范围上操作。注意范围定义为 [first, last)
,其中 last
指代要查询或修改的最后元素的后一个元素。
在一对边界值间夹逼一个值
std::clamp
template<class T> | (1) | (C++17 起) |
template<class T, class Compare> | (2) | (C++17 起) |
1) 若 v
小于 lo
,则返回 lo
;若 v
大于 hi
,则返回 hi
;否则返回 v
。使用 operator< 比较值。
2) 同 (1) ,不过使用 comp
比较值。
若 lo
的值大于 hi
则行为未定义。
参数
v | - | 要夹住的值 |
lo,hi | - | 夹 v 的边界 |
comp | - | 比较函数对象(即满足比较 (Compare) 要求的对象),若 lo 小于 v 且 v 小于 hi ,则返回 true 。 比较函数的签名应等价于如下: bool cmp(const Type1 &a, const Type2 &b); 虽然签名不必有 const & ,函数也不能修改传递给它的对象,而且必须接受(可为 const 的)类型 |
类型要求 | ||
- 为使用重载 (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}