zoukankan      html  css  js  c++  java
  • C++11 lambda函数符

    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<cmath>
    #include<ctime>
    const long Size1 = 39L;
    const long Size2 = 100 * Size1;
    const long Size3 = 100 * Size2;
    bool f3(int x) {return x % 3 == 0 ;}
    bool f13(int x) {return x % 13 == 0;}
    
    int main() {
        using std::cout;
        std::vector<int>numbers(Size1);
        std::srand(std::time(0));
        std::generate(numbers.begin(),numbers.end(),std::rand);
    
        cout << "sample size " << Size1 << std::endl;
        int count3 = std::count_if(numbers.begin(),numbers.end(),f3);
        cout << "count of numbers divisable by 3 " << count3 << std::endl;
        
        int count13 = std::count_if(numbers.begin(),numbers.end(),f13);
        cout << "count of numbers divisable by 13" << count13 << std::endl;
    
        numbers.resize(Size2);
        std::generate(numbers.begin(),numbers.end(),std::rand);
        cout << "sample size" << Size2 << std::endl;
    
        class f_mod {
        private :
        int dv;
        public :
        f_mod(int d = 1) : dv(d) { }
        bool operator () (int x) { return x % dv == 0; }
        };
    
        count3 = std::count_if(numbers.begin(),numbers.end(),f_mod(3));
        cout << "count of number div by 13" << count3 << std::endl;
    
        numbers.resize(Size3);
        std::generate(numbers.begin(),numbers.end(),std::rand);
        cout << "sample size" << Size3 << std::endl;
        count3 = std::count_if(numbers.begin(),numbers.end(),[](int x) {return x % 3 == 0 ; });
        cout << "count3 = " << count3 << std::endl;
    
        return 0;
    }
    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<cmath>
    #include<ctime>
    const long Size = 390000L;
    int main() {
        using std::cout;
        std::vector<int>numbers(Size);
        std::srand(std::time(0));
        std::generate(numbers.begin(),numbers.end(),std::rand);
        cout << "sample size = " << Size << std::endl;
        int count3 = std::count_if(numbers.begin(),numbers.end(),[](int x) {return x % 3 == 0;});
        cout << "count of numbers div by 3  " << count3 << std::endl;
        
        
        int count13 = 0;
        std::for_each(numbers.begin(),numbers.end(),[&count13](int x) { count13 += x % 13 == 0 ; });
        cout << " count 13 " << count13 << std::endl;
        
        count3 = count13 = 0;
        std::for_each(numbers.begin(),numbers.end(),[&] (int x) {count3 += x % 3 == 0 ; count13 += x % 13 == 0 ;});
        cout << "count3 " << count3 << " count13 " << count13 << std::endl;
        
        return 0;
    }
        
  • 相关阅读:
    Firefly官方教程之DBentrust使用文档
    《烽烟OL》简介
    《暗黑世界》简介
    CrossApp简介
    内联元素于与块元素的转换 相对定位、绝对定位以及fixed定位 Z轴覆盖
    div介绍 盒子模型边框属性 CSS初始化 文字排版 边框调整 溢出
    CSS样式补充第二天
    CSS样式
    form表单以及内嵌框架标签
    html对a标签的运用以及属性,img图像标签的属性及应用
  • 原文地址:https://www.cnblogs.com/Commence/p/5687355.html
Copyright © 2011-2022 走看看