在C++ STL中 std::minmax()和std::minmax_element()

在C++ STL中 std::minmax()和std::minmax_element()

我们已经知道C ++编程语言具有一个像大海一样庞大的标准模板库,并且具有许多预定义的功能。要获得通过用户输入或测试人员/开发人员预定义的数组元素中最大或最小的元素,我们使用标准最小值函数或整个函数。

了解同时得知两个值可能有一些重复或多次使用代码的问题。所以C ++ STL为我们提供了 std minmax()和minmax_element()函数的解决方案。这两个函数都接受参数,可以是任何自然数、整数或元素数组的变量形式。

minmax()函数

// Here, we are writing down the C++ programming language to demonstrate 
// the concept of std::minmax() and std::minmax_element() in C++ STL
#include
#include
using namespace std;

int main()
{

// the below code snippet implements declaring the pair
// which is useful in catching the return value thrown after operations
pair mnmx;

// the below code snippet helps us with using min and max functions passed 
// as two parameters
mnmx = minmax(63, 33);

// the below code snippet helps us with using min and max functions passed 
// as two parameters and printing them down on our output screens
cout << "minimum value is : ";
cout << mnmx.first;
cout << "\nmaximum value is : ";
cout << mnmx.second ;

// the below code snippet helps us with using min and max functions passed 
// as two parameters here, they are being given as the array of elements
mnmx = minmax({12, 15, 11, 16, 13});

// the below code snippet helps us with using min and max functions passed 
// as two parameters and printing them down on our output screens
cout << "\nminimum value: ";
cout << mix.first;
cout << "\nmaximum value  : ";
cout << mnmx.second;

}

输出:

/ tmp / 1Lj mBg VyK h.o
The minimum value is : 33
The maximum value is : 63
minimum value : 11
maximum value  : 16

minmax_element() 函数

// Here, we are writing down the C++ programming language to demonstrate 
// the concept of std::minmax() and std::minmax_element() in C++ STL
#include
#include
#include
using namespace std;

int main()
{

    // the below code snippet helps us with vectors being passed as the 
    // data type integer and values ranging from only single integers
    vector vi = { 15, 13, 14, 14, 13, 15, 13 };


    // the below code snippet helps us with declaring the pointer variable
    // of integer data type where one vector being iterator other is a maxx
    // these pointer variables return us a value which will then be caught
    pair::iterator, vector::iterator> mnmx;

    // using minmax_element() to find
    // minimum and maximum element
    // between 0th and 3rd number
    mnmx = minmax_element(vi.begin(), vi.begin() + 14);


    // the below code snippet helps us with using min and max functions 
    //passed 
   // as two parameters and printing their positions down on our output 
   //screens
    cout << "position of the minimum value obtained after operating is : ";
    cout << mnmx.first - vi.begin() << endl;

    cout << "position of the maximum value obtained after operating is : ";
    cout << mnmx.second - vi.begin() << endl;

    cout << endl;



    // here, we are using the duplicates
    // the below code snippet prints us 11 and 15 respectively as outputs

    mnmx = minmax_element(vi.begin(), vi.end());

    // the below code snippet helps us with using min and max functions 
    //passed 
   // as two parameters and printing them down on our output screens

    cout << "position of the minimum value obtained after operating is : ";
    cout << mnmx.first - vi.begin() << endl;

    cout << "position of the maximum value obtained after operating is : ";
    cout << mnmx.second - vi.begin()<< endl;



}

输出:

/ tmp /1 LjmB gVy Kh.o
position of the minimum value obtained after operating is : 7
position of the maximum value received after working is : 10

position of the minimum value obtained after operating is : 1
position of the maximum value received after working is : 5

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程