mem_fun_ref with method from user-defined class : mem_fun_ref « STL Algorithms Helper « C++

Home
C++
1.Bitset
2.Class
3.Console
4.Data Structure
5.Data Type
6.Deque
7.Development
8.File
9.Function
10.Generic
11.Language
12.List
13.Map Multimap
14.Overload
15.Pointer
16.Qt
17.Queue Stack
18.Set Multiset
19.STL Algorithms Binary search
20.STL Algorithms Heap
21.STL Algorithms Helper
22.STL Algorithms Iterator
23.STL Algorithms Merge
24.STL Algorithms Min Max
25.STL Algorithms Modifying sequence operations
26.STL Algorithms Non modifying sequence operations
27.STL Algorithms Sorting
28.STL Basics
29.String
30.Valarray
31.Vector
C / ANSI-C
C Tutorial
C++ Tutorial
Visual C++ .NET
C++ » STL Algorithms Helper » mem_fun_refScreenshots 
mem_fun_ref with method from user-defined class
  
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

class Employee
{
   public:
   Employeestring name = "Unknown"int income = 0int bonus_percentage = );

   int bonusint games_won const;

   void print() const;

   string name() const;
   int income() const;

   private:

   int salary() const;  
   int bonus_percentage_;
   int income_;
   string name_;
};

inline Employee::Employee(string name, int income, int bonus_percentage): bonus_percentage_bonus_percentage ), income_income ),name_name )
{} 

inline int Employee::bonusint games_won const
   return static_cast<int>(salary() bonus_percentage_ / 100.0 * games_won )
}

inline
void Employee::print() const
{
   cout << setw10 << left << name() << "Income: " << setw)
      << right << income() << " euro per year   Bonus: "
      << bonus_percentage_ << "% of salary per game won\n";
}

inline int Employee::income() const
return income_; }

inline
string Employee::name() const
return name_; }

inline
int Employee::salary() const
return static_cast<int>0.3 * income() )}

int main( ){
   vector<Employee> myVector;
   myVector.push_backEmployee"A"4) );
   myVector.push_backEmployee"B"1) );
   myVector.push_backEmployee"C"2) );
   myVector.push_backEmployee"D"3) );
   myVector.push_backEmployee"E"5) );

   for_eachmyVector.begin(), myVector.end(),mem_fun_ref&Employee::print ) );

   vector<int> temporarymyVector.size() );
   transformmyVector.begin(), myVector.end(),temporary.begin(), mem_fun_ref&Employee::income ) );

   int average_income = accumulatetemporary.begin(),temporary.end()/ temporary.size();
      
   cout << myVector.size()
      << " : "
      << average_income << " \n\n";
}
  
    
  
Related examples in the same category
1.Use mem_fun_ref to pass in a user-defined member function
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.