常用的operator classes

  1. void test_plus()  
  2. {  
  3.     // 数组与数组之间求和  
  4.     int first[] = {1, 2, 3, 4, 5};  
  5.     int second[] = {10, 20, 30, 40, 50};  
  6.     int results[5];  
  7.     transform(first, first + 5, second, results, std::plus<int>());  
  8.   
  9.     std::copy(first, first + 5, std::ostream_iterator<int>(std::cout, " "));  
  10.     std::cout << std::endl;  
  11.   
  12.     // 数组累计求和  
  13.     int i[] = {1, 2, 3, 4, 5, 6};  
  14.     int sum(0);  
  15.     sum = accumulate(i, i + 6, 0, std::plus<int>());  
  16.     std::cout << "The result of 1 + 2 + 3 + 4 + 5 + 6 = " << sum << std::endl;  
  17. }  
  18.   
  19. void test_minus()  
  20. {  
  21.     int numbers[] = {10, 20, 30};  
  22.     int result;  
  23.     result = accumulate(numbers, numbers + 3, 100, std::minus<int>());  
  24.     std::cout << "The result of 100 - 10 - 20 - 30 = " << result << std::endl;  
  25. }  
  26.   
  27. void test_multi()  
  28. {  
  29.     int numbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};  
  30.     int result;  
  31.     result = accumulate(numbers, numbers + 10, 1, std::multiplies<int>());  
  32.     std::cout << "The result of 1*2*3*4*5*6*7*8*9*10 = " << result << std::endl;  
  33.   
  34.     // 可以做成阶乘  
  35.     int factorials[10];  
  36.     partial_sum(numbers, numbers + 10, factorials, std::multiplies<int>());  
  37.     std::copy(factorials, factorials + 10, std::ostream_iterator<int>(std::cout, "\n"));  
  38.       
  39.   
  40. //  for (int i = 0; i < 10; ++i)  
  41. //      std::cout << numbers[i] << "! is " << factorials[i] << std::endl;  
  42. }  
  43.   
  44. void test_divides()  
  45. {  
  46.     int first[] = {10, 40, 90, 40, 10};  
  47.     int second[] = {1, 2, 3, 4, 5};  
  48.     int results[5];  
  49.     transform(first, first + 5, second, results, std::divides<int>());  
  50.     std::copy(results, results + 5, std::ostream_iterator<int>(std::cout, " "));  
  51.     std::cout << std::endl;  
  52. }  
  53.   
  54. void test_greater()  
  55. {  
  56.     int numbers[] = {10, 40, 90, 50, 20};  
  57.     std::vector<int> v(numbers, numbers + 5);  
  58.     std::sort(v.begin(), v.end(), std::greater<int>());  
  59.     std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));  
  60. }  
  61.   
  62. // logical_and, logical_or, logical_not应用基本类似.  
  63. void test_logical_and()  
  64. {  
  65.     bool foo[] = {truefalsetruefalse};  
  66.     bool bar[] = {truetruefalsefalse};  
  67.     bool result[4];  
  68.     transform(foo, foo+4, bar, result, std::logical_and<bool>());  
  69.     std::cout << std::boolalpha << "Logical AND:\n";  
  70.     for (int i = 0; i < 4; ++i)  
  71.         std::cout << foo[i] << " AND " << bar[i] << " = " << result[i] << std::endl;  
  72. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值