目录

  • 最小/最大操作(Minimum/maximum operations)
    • 一、max
      • 1、原型:
      • 2、说明:
      • 3、官方demo
    • 二、max_element
      • 1、原型:
      • 2、说明:
      • 3、官方demo
    • 三、min
      • 1、原型:
      • 2、说明:
      • 3、官方demo
    • 四、min_element
      • 1、原型:
      • 2、说明:
      • 3、官方demo
    • 五、minmax
      • 1、原型:
      • 2、说明:
      • 3、官方demo
    • 六、minmax_element
      • 1、原型:
      • 2、说明:
      • 3、官方demo
    • 七、clamp 从C++17开始
      • 1、原型:
      • 2、说明:
      • 3、官方demo
  • 比较运算(Comparison operations)
    • 一、equal
      • 1、原型:
      • 2、说明:
      • 3、官方demo
    • 二、lexicographical_compare
      • 1、原型:
      • 2、说明:
      • 3、官方demo

最小/最大操作(Minimum/maximum operations)

一、max

1、原型:
template< class T >
const T& max( const T& a, const T& b );template< class T, class Compare >
const T& max( const T& a, const T& b, Compare comp );template< class T >
T max( std::initializer_list<T> ilist );template< class T, class Compare >
T max( std::initializer_list<T> ilist, Compare comp );
2、说明:

返回a、b中较大的一个;返回ilist中最大一个。
std::initializer_list 和 vector 类似,不同的是,initializer_list对象中的元素永远是常量值,我们无法改变initializer_list对象中元素的值。

3、官方demo
#include <algorithm>
#include <iostream>
#include <string>int main()
{std::cout << "larger of 1 and 9999: " << std::max(1, 9999) << '\n'<< "larger of 'a', and 'b': " << std::max('a', 'b') << '\n'<< "longest of \"foo\", \"bar\", and \"hello\": " <<std::max( { "foo", "bar", "hello" },[](const std::string& s1, const std::string& s2) {return s1.size() < s2.size();}) << '\n';
}

Output:

larger of 1 and 9999: 9999
larger of 'a', and 'b': b
longest of "foo", "bar", and "hello": hello

二、max_element

1、原型:
template< class ForwardIt >
ForwardIt max_element( ForwardIt first, ForwardIt last );template< class ForwardIt, class Compare >
ForwardIt max_element( ForwardIt first, ForwardIt last, Compare comp );
2、说明:

返回[first, last)中最大的元素的位置(迭代器)

3、官方demo
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>static bool abs_compare(int a, int b)
{return (std::abs(a) < std::abs(b));
}int main()
{std::vector<int> v{ 3, 1, -14, 1, 5, 9 }; std::vector<int>::iterator result;result = std::max_element(v.begin(), v.end());std::cout << "max element at: " << std::distance(v.begin(), result) << '\n';result = std::max_element(v.begin(), v.end(), abs_compare);std::cout << "max element (absolute) at: " << std::distance(v.begin(), result);
}

Output:

max element at: 5
max element (absolute) at: 2

三、min

1、原型:
template< class T >
const T& min( const T& a, const T& b );template< class T, class Compare >
const T& min( const T& a, const T& b, Compare comp );template< class T >
T min( std::initializer_list<T> ilist );template< class T, class Compare >
T min( std::initializer_list<T> ilist, Compare comp );
2、说明:

返回a、b中较小的一个;返回ilist中最小一个。

3、官方demo
#include <algorithm>
#include <iostream>
#include <string>int main()
{std::cout << "smaller of 1 and 9999: " << std::min(1, 9999) << '\n'<< "smaller of 'a', and 'b': " << std::min('a', 'b') << '\n'<< "shortest of \"foo\", \"bar\", and \"hello\": " <<std::min( { "foo", "bar", "hello" },[](const std::string& s1, const std::string& s2) {return s1.size() < s2.size();}) << '\n';
}

Output:

smaller of 1 and 9999: 1
smaller of 'a', and 'b': a
shortest of "foo", "bar", and "hello": foo

四、min_element

1、原型:
template< class ForwardIt >
ForwardIt min_element( ForwardIt first, ForwardIt last );template< class ForwardIt, class Compare >
ForwardIt min_element( ForwardIt first, ForwardIt last, Compare comp );
2、说明:

返回[first, last)范围内,最小元素的位置(迭代器)

3、官方demo
#include <algorithm>
#include <iostream>
#include <vector>int main()
{std::vector<int> v{3, 1, 4, 1, 5, 9};std::vector<int>::iterator result = std::min_element(v.begin(), v.end());std::cout << "min element at: " << std::distance(v.begin(), result);
}

Output:

min element at: 1

五、minmax

1、原型:
template< class T >
std::pair<const T&,const T&> minmax( const T& a, const T& b );template< class T, class Compare >
std::pair<const T&,const T&> minmax( const T& a, const T& b, Compare comp );template< class T >
std::pair<T,T> minmax( std::initializer_list<T> ilist);template< class T, class Compare >
std::pair<T,T> minmax( std::initializer_list<T> ilist, Compare comp );
2、说明:

以std::pair成对返回最小、最大值。

3、官方demo
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>int main()
{std::vector<int> v {3, 1, 4, 1, 5, 9, 2, 6}; std::srand(std::time(0));std::pair<int, int> bounds = std::minmax(std::rand() % v.size(),std::rand() % v.size());std::cout << "v[" << bounds.first << "," << bounds.second << "]: ";for (int i = bounds.first; i < bounds.second; ++i) {std::cout << v[i] << ' ';}std::cout << '\n';
}

Possible output:

v[2,7]: 4 1 5 9 2

六、minmax_element

1、原型:
template< class ForwardIt >
std::pair<ForwardIt,ForwardIt>minmax_element( ForwardIt first, ForwardIt last );template< class ForwardIt, class Compare >
std::pair<ForwardIt,ForwardIt>minmax_element( ForwardIt first, ForwardIt last, Compare comp );
2、说明:

成对(std::pair)返回[first, last)范围内最小、最大值的位置(迭代器)

3、官方demo
#include <algorithm>
#include <iostream>
#include <vector>int main() {const auto v = { 3, 9, 1, 4, 2, 5, 9 };const auto [min, max] = std::minmax_element(begin(v), end(v));std::cout << "min = " << *min << ", max = " << *max << '\n';
}

Output:

min = 1, max = 9

七、clamp 从C++17开始

1、原型:
template<class T>
constexpr const T& clamp( const T& v, const T& lo, const T& hi );template<class T, class Compare>
constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp );
2、说明:

如果v小于lo,返回lo;如果v大于hi,返回hi;否则返回v;即确保返回的值在lo和hi之间。

3、官方demo
#include <cstdint>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <random>int main()
{std::mt19937 g(std::random_device{}());std::uniform_int_distribution<> d(-300, 300);std::cout << " raw   clamped to int8_t   clamped to uint8_t\n";for(int n = 0; n < 5; ++n) {int v = d(g);std::cout << std::setw(4) << v<< std::setw(20) << std::clamp(v, INT8_MIN, INT8_MAX)<< std::setw(21) << std::clamp(v, 0, UINT8_MAX) << '\n';}
}

Possible output:

.raw   clamped to int8_t   clamped to uint8_t
168                 127                  168
128                 127                  128
-137                -128                 040                  40                  40-66                 -66                 0

比较运算(Comparison operations)

一、equal

1、原型:
template< class InputIt1, class InputIt2 >
bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2 );template< class InputIt1, class InputIt2, class BinaryPredicate >
bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p );
2、说明:

判读两个序列是否相同。

3、官方demo
#include <algorithm>
#include <iostream>
#include <string>// 判读是否是回文序列
bool is_palindrome(const std::string& s)
{return std::equal(s.begin(), s.begin() + s.size()/2, s.rbegin());
}void test(const std::string& s)
{std::cout << "\"" << s << "\" "<< (is_palindrome(s) ? "is" : "is not")<< " a palindrome\n";
}int main()
{test("radar");test("hello");
}

Output:

"radar" is a palindrome
"hello" is not a palindrome

二、lexicographical_compare

1、原型:
template< class InputIt1, class InputIt2 >
bool lexicographical_compare( InputIt1 first1, InputIt1 last1,InputIt2 first2, InputIt2 last2 );template< class InputIt1, class InputIt2, class Compare >
bool lexicographical_compare( InputIt1 first1, InputIt1 last1,InputIt2 first2, InputIt2 last2,Compare comp );
2、说明:

按照字典顺序比较第一个序列是否小于第二个序列。

3、官方demo
#include <algorithm>
#include <iostream>
#include <vector>
#include <random>int main()
{std::vector<char> v1 {'a', 'b', 'c', 'd'};std::vector<char> v2 {'a', 'b', 'c', 'd'};std::mt19937 g{std::random_device{}()};while (!std::lexicographical_compare(v1.begin(), v1.end(),v2.begin(), v2.end())) {for (auto c : v1) std::cout << c << ' ';std::cout << ">= ";for (auto c : v2) std::cout << c << ' ';std::cout << '\n';std::shuffle(v1.begin(), v1.end(), g);std::shuffle(v2.begin(), v2.end(), g);}for (auto c : v1) std::cout << c << ' ';std::cout << "< ";for (auto c : v2) std::cout << c << ' ';std::cout << '\n';
}

Possible output:

a b c d >= a b c d
d a b c >= c b d a
b d a c >= a d c b
a c d b < c d a b

【C++】C++11 STL算法(六):最小/最大操作(Minimum/maximum operations)、比较运算(Comparison operations)相关推荐

  1. 【C++】C++11 STL算法(三):分隔操作(Partitioning operations)、排序操作(Sorting operations)

    目录 分隔操作(Partitioning operations) 一.is_partitioned 1.原型: 2.说明: 3.官网demo 二.partition 1.原型: 2.说明: 3.官方d ...

  2. 【C++】C++11 STL算法(五):设置操作(Set operations)、堆操作(Heap operations)

    目录 设置操作(Set operations) 一.includes 1.原型: 2.说明: 3.官方demo 二.set_difference 1.原型: 2.说明: 3.官方demo 三.set_ ...

  3. 【C++】C++11 STL算法(七):排列操作(Permutation operations)、数值操作(Numeric operations)

    排列操作(Permutation operations) 一.is_permutation 1.原型: template< class ForwardIt1, class ForwardIt2 ...

  4. 【C++】C++11 STL算法(一):非修改序列操作(Non-modifying sequence operations)

    目录 一.all_of.any_of.none_of: 1.官方说明 2.谓词 3.STL算法对谓词的说明 4.谓词的五种模式 5.all_of (C++ 11) 6.any_of (C++ 11) ...

  5. 【C++】C++11 STL算法(二):修改序列的操作(Modifying sequence operations)

    目录 一.copy.copy_if 1.原型: 2.说明: 3.官方demo 二.copy_n 1.原型: 2.说明: 3.官方demo 三.copy_backward 1.原型: 1.说明: 1.官 ...

  6. 【C++】C++11 STL算法(四):二分查找法(Binary search operations)、合并操作

    目录 一.lower_bound 1.原型: 2.说明: 3.官方demo 二.upper_bound 1.原型: 2.说明: 3.官方demo 三.binary_search 1.原型: 2.说明: ...

  7. 【C++】C++11 STL算法(十):使用STL实现排序算法

    一.快速排序 1.适用于c++11版本 template <class ForwardIt> void quicksort(ForwardIt first, ForwardIt last) ...

  8. 【C++】C++11 STL算法(九):番外篇

    1.如果获取指针或迭代器指向的类型 详见:C 11:如何获取一个指针或迭代器指向的类型? decltype(*std::declval<Pointer>()) decltype:c++11 ...

  9. 【C++】C++11 STL算法(八):对未初始化内存的操作(Operations on uninitialized memory)、C库(C library)

    对未初始化内存的操作(Operations on uninitialized memory) 一.uninitialized_copy 1.原型: template< class InputIt ...

最新文章

  1. Linux静态/动态链接库的创建和使用
  2. 【网易云信招聘啦】216位攻城狮,呼唤7个好基友
  3. WCF中的方法重载 实现
  4. sklearn下载方法,如果pycharm下载失败的话,你可以看看这里
  5. Win 2003系统传真功能的配置及使用
  6. 如何用android下载python_如何在android上运行Python代码
  7. django怎么在html添加变量,如何在django项目的html模板中创建局部变量?
  8. Linux下基本TCP socket编程之客户端
  9. KeyBlaze for mac(专业打字练习软件)激活版
  10. Java和C语言动态构造int数组
  11. 基于nao机器人实现语音对话(智能版本)
  12. python大作业数据_python 爬虫初探和简单数据分析及可视化,帮学妹写个大作业...
  13. RFB(Receptive Field Block)
  14. 02-linux-arm板上opencv移植--终极解决方案之buildroot基础配置(原创)
  15. 电脑语言栏消失看我怎么办
  16. 设置Chrome浏览器不加载图片的方法
  17. [anjularjs] ui-router嵌套ui-view不刷新问题
  18. 对于洛谷提高试炼场-动态规划篇的爆破
  19. ElasticSearch 定时删除指定天数的数据实践
  20. php一点通,编程一点通下载-编程一点通最新安卓版下载-99wo下载站

热门文章

  1. Linux那些事儿 之 戏说USB(15)设备
  2. jsp ul设置滚动条_jquery实现Li滚动时滚动条自动添加样式的方法
  3. 【请求后台接口】30秒完成Angular10精简版HttpClient请求服务搭建
  4. Python中使用中文正则表达式匹配指定的中文字符串
  5. 【LeetCode】230#二叉搜索树中第K小的元素
  6. JavaScript闭包函数箭头函数调用与执行
  7. alpha阶段个人总结(201521123031林庭亦)
  8. ERROR: from PIL import Image ImportError: No module named PIL
  9. 袋鼠过河(动态规划)
  10. 【BZOJ1015】【JSOI2008】星球大战 并查集