c++ 求數(shù)組最大最小值函數(shù)的實(shí)現(xiàn)
求數(shù)組元素最大最小值函數(shù)
#include<iostream> #include<algorithm> using namespace std; int main() { int a[5]={1,2,3,0,-20}; cout<<*max_element(a,a+5)<<endl; cout<<*max_element(a,a+5)<<endl; return 0; }
也可以通過(guò)這種方式,修改最大值或最小值
#include<iostream> #include<algorithm> using namespace std; int main() { int a[5]={1,2,3,0,-2},m=10; *min_element(a,a+5) += *max_element(a,a+5);//把最小元素和最大元素的和 賦給當(dāng)前最小元素 cout<<*max_element(a,a+5); return 0; }
c++中min和max函數(shù)
包含在c++標(biāo)準(zhǔn)庫(kù)中頭文件<algorithm>中,在頭文件<windows.h>中定義了min,max的宏,若在包含<algorithm>的同時(shí)包含<windows.h>會(huì)導(dǎo)致函數(shù)無(wú)法使用。
<windows.h>提供了_cpp_min等函數(shù)來(lái)代替min函數(shù)的功能。
C++11標(biāo)準(zhǔn):<algorithm>中min函數(shù)的原型
default (1) | template <class T> const T& min (const T& a, const T& b); |
---|---|
custom (2) | template <class T, class Compare> const T& min (const T& a, const T& b, Compare comp); |
initializer list (3) | template <class T> T min (initializer_list<T> il); template <class T, class Compare> T min (initializer_list<T> il, Compare comp); |
Return the smallest
Returns the smallest of a and b. If both are equivalent, a is returned.
The versions for initializer lists (3) return the smallest of all the elements in the list. Returning the first of them if these are more than one.
The function uses operator< (or comp, if provided) to compare the values.
eg:custom2<pre style="margin-top: 0px; margin-bottom: 0px; color: rgb(0, 128, 0);">template <class T, class Compare> ? const T& min (const T& a, const T& b, Compare comp);
#include<iostream> #include<algorithm> using namespace std; struct var { ?? ?char *name; ?? ?int key; ?? ?var(char *a,int k):name(a),key(k){} }; bool comp(const var& l, const var& r) { ?? ?return l.key < r.key; } int main() { ?? ?var v1("var1", 2); ?? ?var v2("var2", 3); ?? ?cout << std::min(v1, v2,comp).name << endl; ?? ?return 0; }
stable_sort,max函數(shù)同min
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
數(shù)據(jù)結(jié)構(gòu)之鏈?zhǔn)蕉鏄?shù)詳解
所謂二叉樹(shù)遍歷 (Traversal) 是按照某種特定的規(guī)則,依次對(duì)二叉樹(shù)中的節(jié)點(diǎn)進(jìn)行相應(yīng)的操作,并且每個(gè)節(jié)點(diǎn)只操作一次。本文通過(guò)代碼示例詳細(xì)介紹了C語(yǔ)言中的鏈?zhǔn)蕉鏄?shù),需要的朋友可以參考一下2023-04-04C語(yǔ)言中const,volatile,restrict的用法總結(jié)
以下是對(duì)C語(yǔ)言中const,volatile,restrict的用法進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過(guò)來(lái)參考下2013-10-10C++寫(xiě)注冊(cè)表項(xiàng)實(shí)例
這篇文章主要介紹了C++寫(xiě)注冊(cè)表項(xiàng)實(shí)例,可實(shí)現(xiàn)開(kāi)機(jī)啟動(dòng)的功能,是進(jìn)行Windows桌面應(yīng)用程序開(kāi)發(fā)中非常重要的技巧,需要的朋友可以參考下2014-10-10C語(yǔ)言中g(shù)etopt()函數(shù)和select()函數(shù)的使用方法
這篇文章主要介紹了C語(yǔ)言中g(shù)etopt()函數(shù)和select()函數(shù)的使用方法,是C語(yǔ)言入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09C語(yǔ)言棧順序結(jié)構(gòu)實(shí)現(xiàn)代碼
一個(gè)能夠自動(dòng)擴(kuò)容的順序結(jié)構(gòu)的棧 ArrStack 實(shí)例 (GCC編譯),有需要的朋友可以參考一下2013-10-10vscode不同項(xiàng)目使用不同的插件的實(shí)現(xiàn)
本文主要介紹了vscode不同項(xiàng)目使用不同的插件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07C語(yǔ)言計(jì)算字符串最后一個(gè)單詞的長(zhǎng)度
大家好,本篇文章主要講的是C語(yǔ)言計(jì)算字符串最后一個(gè)單詞的長(zhǎng)度,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下,方便下次瀏覽2021-12-12