C++/STL Algorithms Helper/mem fun ref
mem_fun_ref with method from user-defined class
<source lang="cpp">
- include <algorithm>
- include <functional>
- include <iomanip>
- include <iostream>
- include <numeric>
- include <string>
- include <vector>
using namespace std; class Employee {
public: Employee( string name = "Unknown", int income = 0, int bonus_percentage = 0 ); int bonus( int 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::bonus( int games_won ) const{
return static_cast<int>(salary() * ( bonus_percentage_ / 100.0 ) * games_won );
} inline void Employee::print() const {
cout << setw( 10 ) << left << name() << "Income: " << setw( 8 ) << 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_back( Employee( "A", 4, 3 ) ); myVector.push_back( Employee( "B", 1, 3 ) ); myVector.push_back( Employee( "C", 2, 3 ) ); myVector.push_back( Employee( "D", 3, 2 ) ); myVector.push_back( Employee( "E", 5, 1 ) ); for_each( myVector.begin(), myVector.end(),mem_fun_ref( &Employee::print ) ); vector<int> temporary( myVector.size() ); transform( myVector.begin(), myVector.end(),temporary.begin(), mem_fun_ref( &Employee::income ) ); int average_income = accumulate( temporary.begin(),temporary.end(), 0 ) / temporary.size(); cout << myVector.size() << " : " << average_income << " \n\n";
}
</source>
Use mem_fun_ref to pass in a user-defined member function
<source lang="cpp">
- include <algorithm>
- include <functional>
- include <iomanip>
- include <iostream>
- include <list>
- include <string>
- include <vector>
using namespace std; class PC {
public: enum part { keyboard, mouse, monitor }; PC( part a_part = PC::keyboard, int id = 0 ); bool operator<( const PC& rhs ) const; void print() const; private: part part_; int id_;
}; inline PC::PC( part a_part, int id ) : part_( a_part ), id_( id ){} inline bool PC::operator<( const PC& rhs ) const{
return id_ < rhs.id_;
} void PC::print() const {
string component; if( part_ == keyboard ) component = "keyboard"; else if( part_ == mouse ) component = "mouse"; else component = "monitor"; cout << "ID: " << setw( 8 ) << left << id_ << " PC: " << component << endl;
} int main( ) {
list<PC> listA; listA.push_back( PC( PC::keyboard, 3 ) ); listA.push_back( PC( PC::mouse, 1 ) ); listA.push_back( PC( PC::monitor, 9 ) ); listA.push_back( PC( PC::keyboard, 2 ) ); listA.push_back( PC( PC::monitor, 8 ) ); for_each( listA.begin(), listA.end(),mem_fun_ref( &PC::print ) );
}
</source>