日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

group + max函数_了解C ++中max()函数的工作方式

發布時間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 group + max函数_了解C ++中max()函数的工作方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

group + max函數

介紹 (Introduction)

Hey there! Today in this tutorial we are going to understand the working as well as the use of the std::max() function in C++.

嘿! 今天,在本教程中,我們將了解C ++中std :: max()函數的工作方式和用法。

std :: max()函數 (The std::max() function)

The std::max() function in C++ is a pre-defined function used to find the maximum of two constant values(int, float, char, string, etc,). It is defined in the <algorithm> header file. The max() function declaration in C++ is given below.

C ++中的std::max()函數是一個預定義函數,用于查找兩個常量值( intfloatcharstring等)中的最大值。 它在<algorithm>頭文件中定義。 C ++中的max()函數聲明如下。

std::max(const T& a, const T&amp; b, Compare comp);

Here, a and b are the two values. And comp(optional) is the comparing function on the basis of which the comparison is to be done.

在此, a和b是兩個值。 comp (可選)是比較函數,將在此基礎上進行比較。

The max() function can also be used for finding the maximum value from an initializer list with an optional comp function. The syntax is given below. Note: this is only valid for C++11 and above versions.

max()函數還可以用于通過可選的comp函數從initializer list查找最大值。 語法如下。 注意:這僅對C ++ 11及更高版本有效。

std::max( std::initializer_list<T> ilist, Compare comp );

使用C ++ max()函數查找最大值 (Finding the Maximum values with C++ max() function)

So now that we have familiarized ourselves with the max() function, let us get into some examples.

現在我們已經熟悉了max()函數,讓我們來看一些示例。

#include<iostream> #include<algorithm> using namespace std; int main() {int a=10, b=20, max;// max stores the maximum integermax = std::max(a, b);cout<<"The max number is:"<<max;return 0; }

Output:

輸出:

The max number is: 20

Here, the max() function returns the maximum value among the passed a and b variables i.e. 20 as expected.

在這里, max()函數返回所傳遞的a和b變量中的最大值,即預期的20

As we mentioned earlier, the function also works for initializer lists. Have a look at the example given below.

如前所述,該函數還適用于initializer lists 。 看下面的例子。

#include<iostream> #include<algorithm> using namespace std;int main() {// with initialiser listcout<<max({ 12, 13, 45, 78, 32});return 0; }

Output:

輸出:

78

We get our desired maximum element 78.

我們得到所需的最大元素78

具有comp函數的C ++ max()函數 (C++ max() function with comp function)

The comp(optional) parameter has to be a function that defines the comparison for the values to find the maximum one. This function further needs to return a bool value on the basis of the provided condition.

comp (可選)參數必須是一個函數,該函數定義值的比較以找到最大值。 此函數還需要根據提供的條件返回bool值。

So, we can either define a lambda function or a user-defined bool function to accomplish this task.

因此,我們可以定義一個lambda function或一個用戶定義的bool函數來完成此任務。

1.作為lambda函數 (1. comp as lambda function)

A lambda function is a one-line user-defined function introduced from C++11 and later versions.

Lambda函數是從C ++ 11和更高版本引入的單行用戶定義函數。

Look at the example given below carefully.

仔細看下面的例子。

#include<iostream> #include<algorithm> using namespace std;int main() {int a=5, b=7;// with lambda functioncout<<max(a, b, [](int a, int b) { return (a < b);});return 0; }

Output:

輸出:

7

Here, we have defined a lambda function that takes two arguments a and b and checks whether the first one is smaller than the other. It returns a boolean value(True or False).

在這里,我們定義了一個lambda函數,該函數接受兩個參數a和b并檢查第一個參數是否小于另一個參數。 它返回一個布爾值( True或False )。

2.作為布爾函數 (2. comp as bool function)

Similarly, we can also define a bool function to compare two arguments.

同樣,我們也可以定義一個bool函數來比較兩個參數。

#include<iostream> #include<algorithm> using namespace std;bool Myfunc(int a, int b) {return (a<b); }int main() {int a=118, b=52;cout<<max(a, b, Myfunc);return 0; }

Output:

輸出:

118

As you can see, we get our desired output(118)

如您所見,我們獲得了所需的輸出( 118

結論 (Conclusion)

That’s it for today. Hope you had a clear understanding of the std::max() function in C++.

今天就這樣。 希望您對C ++中的std :: max()函數有一個清晰的了解

For any related questions, feel free to use the comments below.

如有任何相關問題,請隨時使用以下評論。

參考資料 (References)

  • std::max() – CPP reference,

    std :: max() – CPP參考,
  • Lambda functions in C++ – Microsoft Documentation,

    C ++中的Lambda函數 – Microsoft文檔,
  • C++ Tutorial.

    C ++教程 。

翻譯自: https://www.journaldev.com/40922/max-function-c-plus-plus

group + max函數

總結

以上是生活随笔為你收集整理的group + max函数_了解C ++中max()函数的工作方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。