首页 > 编程语言 > 详细

泛型算法(四)之计数算法

时间:2016-01-19 10:33:08      阅读:169      评论:0      收藏:0      [点我收藏+]

1、count(InputIterator first, InputIterator last, const T& val):序列中等于给定值的元素的计数

    std::vector<int> c;
    c.reserve(10);
    //向c中添加元素
    for (int i = 0; i < 10; i++)
    {
        c.push_back(i * 0);
    }
    //计算c中元素值等于0的元素个数
    int count = std::count(c.begin(), c.end(), 0);
    //输出
    std::cout << count;
    //打印结果:10

2、count_if(InputIterator first, InputIterator last, UnaryPredicate pred):序列中满足给定谓词的元素的计数

    std::vector<int> c;
    c.reserve(10);
    //向c中添加元素
    for (int i = 0; i < 10; i++)
    {
        c.push_back(i);
    }
    //计算c中元素值大于5的元素个数
    int count = std::count_if(c.begin(), c.end(), [](int element){
        return element > 5;
    });
    //输出
    std::cout << count;
    //打印结果:4

 

泛型算法(四)之计数算法

原文:http://www.cnblogs.com/dongerlei/p/5141173.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!