Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Item
- {
- int a;
- int b;
- int c;
- int SpecialB()
- {
- return a * b + c;
- }
- };
- class ItemList : public std::vector<Item>
- {
- int MaxA()
- {
- if( this->empty() )
- throw;
- int maxA = (*this)[0].a;
- for( int idx = 1; idx < this->size(); idx++ )
- {
- if( (*this)[idx].a > maxA )
- {
- maxA = (*this)[idx].a;
- }
- }
- return maxA;
- }
- int SpecialB()
- {
- if( this->empty() )
- throw;
- int specialB = (*this)[0].SpecialB();
- for( int idx = 1; idx < this->size(); idx++ )
- {
- if( (*this)[idx].SpecialB() < specialB )
- {
- specialB -= (*this)[idx].c;
- }
- }
- return specialB;
- }
- int AvgC()
- {
- if( this->empty() )
- throw;
- int cSum = 0;
- for( int idx = 0; idx < this->size(); idx++ )
- {
- cSum += (*this)[idx].c;
- }
- return cSum / this->size(); // average
- }
- };
- int max_a = std::max_element
- (
- v.begin(),
- v.end(),
- boost::bind(
- std::less< int >(),
- bind( &Item::a, _1 ),
- bind( &Item::a, _2 )
- )
- )->a;
- const double avg_c = std::accumulate( v.begin(), v.end(), double( 0 ), boost::bind( Item::c, _1 ) ) / v.size(); // ofcourse check size before divide
- int accumulate_func( int start_from, int result, const Item& item )
- {
- if ( item.SpecialB() < start_from )
- {
- result -= item.SpecialB();
- }
- return result;
- }
- if ( v.empty() )
- {
- throw sometghing( "empty vector" );
- }
- const int result = std::accumulate( v.begin(), v.end(), v.front(), boost::bind( &accumulate_func, v.front(), _1, _2 ) );
- struct CmpA {
- bool operator()(const Item &item1, const Item &item2) { return item1.a < item2.a; }
- }
- const int result = std::max_element(v.begin(), v.end(), CmpA()).a;
- int MaxA(const std::vector<Item>& vec) {
- if(vec.empty()) {
- throw;
- }
- int maxA = vec[0].a;
- for(std::vector<Item>::const_iterator i = vec.begin(); i != vec.end(); ++i) {
- if(i->a > maxA) {
- maxA = i->a;
- }
- }
- return maxA;
- }
- class ItemList {
- private:
- std::vector< Item > mItems;
- public:
- typedef std::vector< Item >::size_type size_type;
- int MaxA();
- int SpecialB();
- Item &operator[]( size_type offset ) { return mItems[offset]; }
- size_type size() const { return mItems.size(); }
- };
Add Comment
Please, Sign In to add comment