zoukankan      html  css  js  c++  java
  • 算法

    一.count函数 
    algorithm头文件定义了一个count的函数,返回这个值出现次数的统计结果。 count : 在序列中统计某个值出现的次数

    c=count(a.begin() , a.end() , x);//返回元素值为x的元素个数。
    #include <iostream>
    #include<algorithm> 
    #include<vector>
    int main()
    {
        std::vector<int>a = { 1,2,3,3 };int c = count(a.begin(), a.end(), 3);//c=2
        std::cout << c<< std::endl;
    }

    二、count_if函数:返回符合一定条件的元素个数。

     

    #include <vector>
    #include <algorithm>
    #include<iostream>
    using namespace std;
    int main()
    {
        vector<int> coll = { 1,2,3,4,5,6,7};
        int c = count_if(coll.begin(), coll.end(), [](int x) {return x > 3; });
        cout << c << endl;//c=4
    }

    三、find、find_if与find_if_not函数

    find查找“与被传入值相等”的第一个元素 

    find_if查找“满足某个准则”的第一个元素 

    find_if查找“不满足某个准则”的第一个元素 

    #include <vector>
    #include <algorithm>
    #include<iostream>
    using namespace std;
    bool comp(int x)
    {
        return x > 5;
    }
    int main()
    {
        vector<int> coll = { 1,2,3,4,8,5,6,7};
        vector<int>::iterator vec_it0, vec_it1, vec_it2;
        vec_it0 = find(coll.begin(),coll.end(),6);
        vec_it1 = find_if(coll.begin(), coll.end(), [](int x) {return x > 5; });
        vec_it2 = find_if(coll.begin(), coll.end(), comp);
        cout << *vec_it0 << " " << *vec_it1 << " " << *vec_it2 << endl;//*vec_it0=6,*vec_it1=8*vec_it2=8
    }
  • 相关阅读:
    if 语句运用
    c#语言基础
    关于条件运算符的应用
    Could not open Hibernate Session for transaction;
    node to traverse cannot be null!
    Struts2文件下载中文名乱码
    Spring MVC框架下的第一个Hello World程序
    strtus2中的defaultactionref无效的解决方法
    c 笔记
    工具系列1
  • 原文地址:https://www.cnblogs.com/hsy1941/p/12433962.html
Copyright © 2011-2022 走看看