c++におけるminとmax関数
1405 ワード
c++標準ライブラリに含まれるヘッダファイルには、min,maxのマクロがヘッダファイルに定義されており、同時に含まれると関数が使用できなくなります.
提供されました_cpp_minなどの関数はmin関数の機能に代わる.
C++11標準:中min関数のプロトタイプ
default(1)
template const T& min (const T& a, const T& b);
custom (2)
template const T& min (const T& a, const T& b, Compare comp);
initializer list(3)
template T min (initializer_list il); template T min (initializer_list 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.
提供されました_cpp_minなどの関数はmin関数の機能に代わる.
C++11標準:中min関数のプロトタイプ
default(1)
template const T& min (const T& a, const T& b);
custom (2)
template const T& min (const T& a, const T& b, Compare comp);
initializer list(3)
template T min (initializer_list il); template T min (initializer_list il, Compare comp);
The function uses operator< (or comp, if provided) to compare the values.
eg:custom2template
const T& min (const T& a, const T& b, Compare comp);
#include
#include
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 min