c++中标准库max算法

#include #include #include #include using namespace std;

boolabs_max(int elem1,int elem2) { if(elem1<0) elem1=-elem1; if(elem2<0) elem2=-elem2; return elem1

classCInt;

ostream& operator<<(ostream&osIn,constCInt&rhs);

classCInt {

private: intm_nVal; public: CInt(int n=0):m_nVal(n){} CInt(constCInt&rhs):m_nVal(rhs.m_nVal){} CInt& operator=(constCInt&rhs) { m_nVal=rhs.m_nVal; return *this; } bool operator<(constCInt&rhs)const//注意const标志 { returnm_nVal

inlineostream& operator<<(ostream&osIn,constCInt&rhs) {

cout<<\ returnosIn; }

voidalg_max();

//============================================================== int main() { alg_max(); return 0; }

voidalg_max() { cout<<\ int a=6,b=-7; constint&Result1=max(a,b); cout<<\ constint&Result2=max(a,b,abs_max); cout<<\ // Comparing set containers with elements of type CInt // using the max algorithm CInt c1=1,c2=2,c3=3; set s1,s2,s3; set::iterator s1_Iter,s2_Iter,s3_Iter; s1.insert(c1); s1.insert(c2); s2.insert(c2); s2.insert(c3); cout<<\ for(s1_Iter=s1.begin();s1_Iter!=s1.end();s1_Iter++) cout<<\ s1_Iter=--s1.end(); cout<<\

cout<<\

for(s2_Iter=s2.begin();s2_Iter!=s2.end();s2_Iter++) cout<<\s2_Iter=--s2.end();

cout<<\s3=max(s1,s2);

cout<<\

for(s3_Iter=s3.begin();s3_Iter!=s3.end();s3_Iter++) cout<<\s3_Iter=--s3.end();

cout<<\

// Comparing vectors with integer elements using the max algorithm //the max algorithm

vector v1,v2,v3,v4,v5;

vector::iterator Iter1,Iter2,Iter3,Iter4,Iter5; inti;

for(i=0;i<=2;i++) { v1.push_back(i);//初始化向量v1 }

int ii;

for(ii=0;ii<=2;ii++) { v2.push_back(ii);//初始化向量v2 }

int iii;

for(iii=0;iii<=2;iii++) { }

v3.push_back(2*iii);//初始化向量v3

cout<<\输出向量v1中的所有元素 for(Iter1=v1.begin();Iter1!=v1.end();Iter1++) cout<<*Iter1<<\cout<<\

cout<<\输出向量v2中的所有元素

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4