std::min, std::max, std::swap, std::rel_ops的简单使用

下面是看书后写的几个小例子:

std::max

[cpp]  view plain copy
  1. #include <algorithm>  
  2. #include <iostream>  
  3. using namespace std;  
  4.   
  5. bool ptr(int* a, int* b) {  
  6.     return *a < *b;  
  7. }  
  8.   
  9. int main() {  
  10.     int x = 11;  
  11.     int y = 12;  
  12.     cout << "max value: " << max(11, 12) << endl;  
  13.     int* px = &x;  
  14.     int* py = &y;  
  15.   
  16.     cout << "max ptr value: " << *max(px, py, ptr) << endl;  
  17. }  

运行结果:

[cpp]  view plain copy
  1. max value: 12  
  2. max ptr value: 12  

std::swap练习

[cpp]  view plain copy
  1. #include <algorithm>  
  2. #include <iostream>  
  3. using std::cout;  
  4. using std::endl;  
  5. using std::ostream;  
  6.   
  7. class MyContainer {  
  8.     int* elems;  
  9.     int numElems;  
  10.   
  11. public:  
  12.     MyContainer(int * array, int num) {  
  13.         elems = array;  
  14.         numElems = num;  
  15.     }  
  16.     void swap(MyContainer& mc) {  
  17.         std::swap(elems, mc.elems);  
  18.         std::swap(numElems, mc.numElems);  
  19.     }  
  20.     friend ostream& operator<<(ostream&, const MyContainer&);  
  21. };  
  22.   
  23. ostream& operator<<(ostream& os, const MyContainer& mc) {  
  24.     for(int i = 0; i < mc.numElems; ++i) {  
  25.         os << *(mc.elems + i) << " ";  
  26.     }  
  27. //  os << endl;  
  28.     return os;  
  29. }  
  30.   
  31. inline void swap(MyContainer& c1, MyContainer& c2) {  
  32.     c1.swap(c2);  
  33. }  
  34.   
  35. int main() {  
  36.     int a[10];  
  37.     int b[10];  
  38.     for(int i = 0; i < 10; ++i) {  
  39.         a[i] = i * i;  
  40.         b[i] = (9 -i) * (9 - i) * (9 - i);  
  41.     }  
  42.     MyContainer mc_a(a, 10);  
  43.     MyContainer mc_b(b, 10);  
  44.     cout << "before swap: " << endl;;  
  45.     cout << "mc_a: " << mc_a << endl;  
  46.     cout << "mc_b: " << mc_b << endl;  
  47.     swap(mc_a, mc_b);  
  48.     cout << "after swap: " << endl;  
  49.     cout << "mc_a: " << mc_a << endl;  
  50.     cout << "mc_b: " << mc_b << endl;  
  51. }  

运行结果:

[cpp]  view plain copy
  1. before swap:  
  2. mc_a: 0 1 4 9 16 25 36 49 64 81  
  3. mc_b: 729 512 343 216 125 64 27 8 1 0  
  4. after swap:  
  5. mc_a: 729 512 343 216 125 64 27 8 1 0  
  6. mc_b: 0 1 4 9 16 25 36 49 64 81  

std::rel_ops练习:

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <utility>  
  3. #include <string>  
  4.   
  5. using std::cout;  
  6. using std::endl;  
  7. using std::string;  
  8.   
  9. class name {  
  10.     string firstName;  
  11.     string lastName;  
  12. public:  
  13.     name(const string& first,  
  14.         const string& last) {  
  15.             firstName = first;  
  16.             lastName = last;  
  17.     }  
  18.     bool operator==(const name& rhs) const {  
  19.         return (firstName == rhs.firstName &&   
  20.             lastName == rhs.lastName);  
  21.     }  
  22.     bool operator<(const name& rhs) const {  
  23.         return (firstName < rhs.firstName ||   
  24.             (firstName == rhs.firstName && lastName < rhs.lastName));  
  25.     }  
  26. };  
  27. int main() {  
  28.     using namespace std::rel_ops;  
  29.     name n1("John""Brown");  
  30.     name n2("Henry""Brown");  
  31.     cout << ((n1 > n2) ? "n1 is bigger than n2" : "n1 is smaller than n2") << endl;  
  32.     name n3("John""Brown");  
  33.     cout << ((n3 != n1) ? "n3 is not equal to n1" : "n3 is equal to n1") << endl;  
  34. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值