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

【C++】郭老二博文之:C++目录

最小/最大操作(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: 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 
#include 
#include 
#include 
 
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 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: 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 
#include 
#include 
 
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 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<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
	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_t
168                 127                  168
128                 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<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++,算法,开发语言)