博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【C++】C++11 STL算法(六):最小/最大操作(Minimum/maximum operations)、比较运算(Comparison operations)
阅读量:4262 次
发布时间:2019-05-26

本文共 8345 字,大约阅读时间需要 27 分钟。

目录

最小/最大操作(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
ilist );template< class T, class Compare >T max( std::initializer_list
ilist, Compare comp );
2、说明:

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

std::initializer_list 和 vector 类似,不同的是,initializer_list对象中的元素永远是常量值,我们无法改变initializer_list对象中元素的值。

3、官方demo
#include 
#include
#include
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: 9999larger of 'a', and 'b': blongest 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 
#include
#include
#include
static bool abs_compare(int a, int b){
return (std::abs(a) < std::abs(b));} int main(){
std::vector
v{
3, 1, -14, 1, 5, 9 }; std::vector
::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: 5max 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
ilist );template< class T, class Compare >T min( std::initializer_list
ilist, Compare comp );
2、说明:

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

3、官方demo
#include 
#include
#include
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: 1smaller of 'a', and 'b': ashortest 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 
#include
#include
int main(){
std::vector
v{
3, 1, 4, 1, 5, 9}; std::vector
::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
minmax( const T& a, const T& b );template< class T, class Compare >std::pair
minmax( const T& a, const T& b, Compare comp );template< class T >std::pair
minmax( std::initializer_list
ilist);template< class T, class Compare >std::pair
minmax( std::initializer_list
ilist, Compare comp );
2、说明:

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

3、官方demo
#include 
#include
#include
#include
#include
int main(){
std::vector
v { 3, 1, 4, 1, 5, 9, 2, 6}; std::srand(std::time(0)); std::pair
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
minmax_element( ForwardIt first, ForwardIt last ); template< class ForwardIt, class Compare >std::pair
minmax_element( ForwardIt first, ForwardIt last, Compare comp );
2、说明:

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

3、官方demo
#include 
#include
#include
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
constexpr const T& clamp( const T& v, const T& lo, const T& hi );template
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 
#include
#include
#include
#include
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_t168                 127                  168128                 127                  128-137                -128                 0 40                  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 
#include
#include
// 判读是否是回文序列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 
#include
#include
#include
int main(){
std::vector
v1 {
'a', 'b', 'c', 'd'}; std::vector
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

转载地址:http://zdmei.baihongyu.com/

你可能感兴趣的文章
C语言 寻找数据中的众数
查看>>
设置cmd命令窗口的起始位置
查看>>
写给四年前刚开始编程的自己
查看>>
谷歌面试题,你敢回答吗。
查看>>
Android 安全攻防(一):SEAndroid的编译
查看>>
Android 安全攻防(二): SEAndroid bionic
查看>>
cuda如何做软连接切换
查看>>
小飞机的一些设置解释
查看>>
目标检测里正负样本和数据增广
查看>>
python和C++的交互方式
查看>>
学英语以及中文快速阅读的启迪,从“为什么全世界只有中日两个国家弹幕视频网站成为流行?”说开去
查看>>
什么是人工神经网络
查看>>
神经网络的发展历史
查看>>
TED演讲:Jeff Hawkins.大脑的工作原理是什么
查看>>
Windows命令行提示
查看>>
梳理《前目的地》
查看>>
ArrayList底层实现
查看>>
ACM寒假培训——各种排序
查看>>
CF417D——Cunning Gena(状态压缩DP)
查看>>
HDU1074——Doing Homework(状态压缩DP)
查看>>