首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

std函数有关问题//求用途。

2013-08-16 
std函数问题//求用途。。。for_each mem_fun_reffind_if bind2nd//例如,这个 函数俩个参数 只有一个自己传值,

std函数问题//求用途。。。


for_each 

mem_fun_ref

find_if 

bind2nd
//例如,这个 函数俩个参数 只有一个自己传值,另一个bind2nd函数自己传值//有意思吗????

[解决办法]
仿函数,又叫函数对象,就是定义一个类,这个类实现了函数调用运算符 
operator ()(....) 有无数种形式,无参,单参数,双参数,N个参数等等
这样这个类的对象,就可以当作函数使用
比如

//仿函数类 
#include <iostream>

using namespace std;
//仿函数类 
class Add{
public:
Add(int x):_x(x){}
int operator()(int y)const {return _x+y;};// 单参数函数调用运算符。
int operator()(int y,int z)const{return _x+y+z;};//双参数的函数调用运算符。
private:
 int _x;
};

int sum (int x,int y,int z){
  return Add(x)(y,z);
}
int sum(int x,int y,const Add& add)
{
   return add(x,y);

int main()
{
     int x=10,y=11,z=120;
     Add a(x);//a 就是函数对象,就是仿函数,英文 functor,意思是这个变量可以当做函数使用。
     cout<<"x + y="<<a(y)<<endl;//仿函数一般用法,当作函数使用 
     cout<<"x + y + z ="<<a(y,z)<<endl;
     cout <<Add(x)(x,y)<<endl;  
     cout<< sum(x,y,z)<<endl;
     cout<< sum(x,y,Add(x))<<endl;
     return 0; 
}



mem_fun_ref 与
mem_fun

mem_fun_ref的示例
VS2008,MSDN,VC 示例 

// functional_mem_fun_ref.cpp
// compile with: /EHsc
#include <vector>
#include <functional>
#include <algorithm>
#include <iostream>

using namespace std;

class NumVals
   {
   int val;
   public:
   NumVals ( ) { val = 0; }


   NumVals ( int j ) { val = j; }

   bool display ( ) { cout << val << " "; return true; }
   bool isEven ( ) { return ( bool )  !( val %2 ); }
   bool isPrime ( ) 
   {
      for ( int i = 2;  i <= ( val / 2 ); i++ )
         if ( !( val % i ) ) return false;
      return true;
   }
};

int main( )
{
   vector <NumVals> v1 ( 13 ), v2 ( 13 );
   vector <NumVals>::iterator v1_Iter, v2_Iter;
   int i, k;

   for ( i = 0; i < 13; i++ ) v1 [ i ] = NumVals ( i+1 );
   for ( k = 0; k < 13; k++ ) v2 [ k ] = NumVals ( k+1 );

   cout << "The original values stored in v1 are: " ;
   for_each( v1.begin( ), v1.end( ), 
   mem_fun_ref ( &NumVals::display ) );
   cout << endl;

   // Use of mem_fun_ref calling member function through a reference
   // remove the primes in the vector using isPrime ( )
   v1_Iter = remove_if ( v1.begin( ),  v1.end( ), 
      mem_fun_ref ( &NumVals::isPrime ) );   
   cout << "With the primes removed, the remaining values in v1 are: " ;
   for_each( v1.begin( ), v1_Iter, 
   mem_fun_ref ( &NumVals::display ) );
   cout << endl;

   cout << "The original values stored in v2 are: " ;
   for_each( v2.begin( ), v2.end( ), 
   mem_fun_ref ( &NumVals::display ) );


   cout << endl;

   // Use of mem_fun_ref calling member function through a reference
   // remove the even numbers in the vector v2 using isEven ( )
   v2_Iter = remove_if ( v2.begin( ),  v2.end( ), 
      mem_fun_ref ( &NumVals::isEven ) );   
   cout << "With the even numbers removed, the remaining values are: " ;
   for_each( v2.begin( ),  v2_Iter, 
   mem_fun_ref ( &NumVals::display ) );
   cout << endl;
}

 
  Copy Code 
The original values stored in v1 are: 1 2 3 4 5 6 7 8 9 10 11 12 13 
With the primes removed, the remaining values in v1 are: 4 6 8 9 10 12 
The original values stored in v2 are: 1 2 3 4 5 6 7 8 9 10 11 12 13 
With the even numbers removed, the remaining values are: 1 3 5 7 9 11 13 

mem_fun_ref的示例
VS2008,MSDN,VC 示例 


// functional_mem_fun.cpp
// compile with: /EHsc
#include <vector>
#include <functional>
#include <algorithm>
#include <iostream>

using namespace std;

class StoreVals   
{
    int val;
public:
    StoreVals() { val = 0; }
    StoreVals(int j) { val = j; }

    bool display() { cout << val << " "; return true; }
    int squareval() { val *= val; return val; }
    int lessconst(int k) {val -= k; return val; }
};

int main( )
{
    vector<StoreVals *> v1;

    StoreVals sv1(5);
    v1.push_back(&sv1);
    StoreVals sv2(10);
    v1.push_back(&sv2);


    StoreVals sv3(15);
    v1.push_back(&sv3);
    StoreVals sv4(20);
    v1.push_back(&sv4);
    StoreVals sv5(25);
    v1.push_back(&sv5);

    cout << "The original values stored are: " ;
    for_each(v1.begin(), v1.end(), mem_fun<bool, StoreVals>(&StoreVals::display));
    cout << endl;

    // Use of mem_fun calling member function through a pointer
    // square each value in the vector using squareval ()
    for_each(v1.begin(), v1.end(), mem_fun<int, StoreVals>(&StoreVals::squareval));   
    cout << "The squared values are: " ;
    for_each(v1.begin(), v1.end(), mem_fun<bool, StoreVals>(&StoreVals::display));
    cout << endl;

    // Use of mem_fun1 calling member function through a pointer
    // subtract 5 from each value in the vector using lessconst ()
    for_each(v1.begin(), v1.end(), 
        bind2nd (mem_fun1<int, StoreVals,int>(&StoreVals::lessconst), 5));   
    cout << "The squared values less 5 are: " ;
    for_each(v1.begin(), v1.end(), mem_fun<bool, StoreVals>(&StoreVals::display));
    cout << endl;
}

Output:
The original values stored are: 5 10 15 20 25 
The squared values are: 25 100 225 400 625 
The squared values less 5 are: 20 95 220 395 620 

热点排行