当前位置:   article > 正文

二元函数对象和二元谓词_二元谓词函数大小

二元谓词函数大小
  1. #include <iostream>
  2. #include<string>
  3. #include <vector>
  4. #include <list>
  5. #include <set>
  6. #include <map>
  7. #include <algorithm>
  8. #include <functional>
  9. using namespace std;
  10. template<typename T>
  11. class SumAdd
  12. {
  13. public:
  14. SumAdd()
  15. {
  16. }
  17. T operator()(const T &i,const T &j)
  18. {
  19. return i+j;
  20. }
  21. protected:
  22. private:
  23. };
  24. bool MyCompare(int i,int j)
  25. {
  26. return i<j;
  27. }
  28. void showElement(int i)
  29. {
  30. cout<<i<<" ";
  31. }
  32. void display()
  33. {
  34. vector<int> v1;
  35. v1.push_back(10);
  36. v1.push_back(4);
  37. v1.push_back(3);
  38. v1.push_back(2);
  39. v1.push_back(5);
  40. v1.push_back(7);
  41. v1.push_back(6);
  42. v1.push_back(1);
  43. vector<int> v2;
  44. v2.push_back(1);
  45. v2.push_back(2);
  46. v2.push_back(3);
  47. v2.push_back(4);
  48. v2.push_back(5);
  49. v2.push_back(6);
  50. v2.push_back(7);
  51. v2.push_back(10);
  52. vector<int> v3(8);
  53. vector<int> v4;
  54. v4.push_back(10);
  55. v4.push_back(4);
  56. v4.push_back(3);
  57. v4.push_back(2);
  58. v4.push_back(5);
  59. v4.push_back(7);
  60. v4.push_back(6);
  61. v4.push_back(1);
  62. SumAdd<int> sumadd;
  63. /*
  64. template<class _InIt1,
  65. class _InIt2,
  66. class _OutIt,
  67. class _Fn2> inline
  68. _OutIt transform(_InIt1 _First1, _InIt1 _Last1,
  69. _InIt2 _First2, _OutIt _Dest, _Fn2 _Func)
  70. { // transform [_First1, _Last1) and [_First2, ...) with _Func
  71. _DEBUG_RANGE(_First1, _Last1);
  72. _DEBUG_POINTER(_Dest);
  73. _DEBUG_POINTER(_Func);
  74. if (_First1 != _Last1)
  75. return (_Transform2(_Unchecked(_First1), _Unchecked(_Last1),
  76. _First2, _Dest, _Func,
  77. _Is_checked(_Dest)));
  78. return (_Dest);
  79. }
  80. */
  81. //返回值是v3.end()
  82. vector<int>::iterator it = transform(v1.begin(),v1.end(),v2.begin(),v3.begin(),sumadd);
  83. cout<<*(--it)<<endl;
  84. for (it = v3.begin();it!=v3.end();it++)
  85. {
  86. cout<<*it<<" ";
  87. }
  88. cout<<"----------------"<<endl;
  89. //把运算结果保存在v1里
  90. vector<int>::iterator it2 = transform(v1.begin(),v1.end(),v2.begin(),v1.begin(),sumadd);
  91. cout<<*(--it2)<<endl;
  92. for (it2 = v1.begin();it2!=v1.end();it2++)
  93. {
  94. cout<<*it2<<" ";
  95. }
  96. cout<<endl;
  97. //排序v4
  98. cout<<"sort--------------"<<endl;
  99. sort(v4.begin(),v4.end(),MyCompare);
  100. //遍历
  101. for_each(v4.begin(),v4.end(),showElement);
  102. }
  103. int main()
  104. {
  105. display();
  106. system("pause");
  107. return 0;
  108. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/589658
推荐阅读
相关标签
  

闽ICP备14008679号