Guest User

Untitled

a guest
Jun 25th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. class Item
  2. {
  3. int a;
  4. int b;
  5. int c;
  6.  
  7. int SpecialB()
  8. {
  9. return a * b + c;
  10. }
  11. };
  12.  
  13. class ItemList : public std::vector<Item>
  14. {
  15. int MaxA()
  16. {
  17. if( this->empty() )
  18. throw;
  19.  
  20. int maxA = (*this)[0].a;
  21.  
  22. for( int idx = 1; idx < this->size(); idx++ )
  23. {
  24. if( (*this)[idx].a > maxA )
  25. {
  26. maxA = (*this)[idx].a;
  27. }
  28. }
  29. return maxA;
  30. }
  31.  
  32. int SpecialB()
  33. {
  34. if( this->empty() )
  35. throw;
  36.  
  37. int specialB = (*this)[0].SpecialB();
  38.  
  39. for( int idx = 1; idx < this->size(); idx++ )
  40. {
  41. if( (*this)[idx].SpecialB() < specialB )
  42. {
  43. specialB -= (*this)[idx].c;
  44. }
  45. }
  46. return specialB;
  47. }
  48.  
  49. int AvgC()
  50. {
  51. if( this->empty() )
  52. throw;
  53.  
  54. int cSum = 0;
  55. for( int idx = 0; idx < this->size(); idx++ )
  56. {
  57. cSum += (*this)[idx].c;
  58. }
  59.  
  60. return cSum / this->size(); // average
  61. }
  62. };
  63.  
  64. int max_a = std::max_element
  65. (
  66. v.begin(),
  67. v.end(),
  68. boost::bind(
  69. std::less< int >(),
  70. bind( &Item::a, _1 ),
  71. bind( &Item::a, _2 )
  72. )
  73. )->a;
  74.  
  75. const double avg_c = std::accumulate( v.begin(), v.end(), double( 0 ), boost::bind( Item::c, _1 ) ) / v.size(); // ofcourse check size before divide
  76.  
  77. int accumulate_func( int start_from, int result, const Item& item )
  78. {
  79. if ( item.SpecialB() < start_from )
  80. {
  81. result -= item.SpecialB();
  82. }
  83. return result;
  84. }
  85.  
  86. if ( v.empty() )
  87. {
  88. throw sometghing( "empty vector" );
  89. }
  90. const int result = std::accumulate( v.begin(), v.end(), v.front(), boost::bind( &accumulate_func, v.front(), _1, _2 ) );
  91.  
  92. struct CmpA {
  93. bool operator()(const Item &item1, const Item &item2) { return item1.a < item2.a; }
  94. }
  95.  
  96. const int result = std::max_element(v.begin(), v.end(), CmpA()).a;
  97.  
  98. int MaxA(const std::vector<Item>& vec) {
  99. if(vec.empty()) {
  100. throw;
  101. }
  102.  
  103. int maxA = vec[0].a;
  104. for(std::vector<Item>::const_iterator i = vec.begin(); i != vec.end(); ++i) {
  105. if(i->a > maxA) {
  106. maxA = i->a;
  107. }
  108. }
  109. return maxA;
  110. }
  111.  
  112. class ItemList {
  113. private:
  114. std::vector< Item > mItems;
  115. public:
  116. typedef std::vector< Item >::size_type size_type;
  117. int MaxA();
  118. int SpecialB();
  119. Item &operator[]( size_type offset ) { return mItems[offset]; }
  120. size_type size() const { return mItems.size(); }
  121. };
Add Comment
Please, Sign In to add comment