zoukankan      html  css  js  c++  java
  • mem_fun 例子

    // functional_mem_fun.cpp
    // compile with: /EHsc
    #include <vector>
    #include <functional>
    #include <algorithm>
    #include <iostream>
    #include <ext/functional>
    
    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 (__gnu_cxx::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;
    }
  • 相关阅读:
    BackgroundWorker原理剖析
    委托异步调用时BeginInvoke的陷阱处理
    线程静态在对象缓存中的妙用
    值得珍藏的.NET源码,不保存就没机会了
    .NET 4.5.1 参考源码索引
    .NET 4.5 参考源码索引
    .NET 4.0 参考源码索引
    WWF3.5SP1 参考源码索引
    WCF3.5 SP1 参考源码索引
    .NET 3.5.1 参考源码索引
  • 原文地址:https://www.cnblogs.com/huashiyiqike/p/3928176.html
Copyright © 2011-2022 走看看