zoukankan      html  css  js  c++  java
  • C++泛型算法迭代器,find,count,accumulate,fill,equal

    一、find,count,查找和计数

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    #include <algorithm>

    using namespace std;

    int main(int argc, char**argv)

    {

        //泛型算法find 函数,元素查找

         //vector

         vector<int> vec{23,456,654,7,65,89,65,89,65,98};

         auto res = find(vec.begin(),vec.end(),65);

         if(res==vec.end()) //尾后迭代器用来判断是否找到元素

         {

             cout<<"没有找到元素"<<endl;

         }

         else

         {

             cout<<*res<<endl;

         }

         //数组

         int ia[]={23,567,75,64,242,57,32,45};

         int *result=find(begin(ia),end(ia),32);//数组指针

         cout<<*result<<endl;

         //数组

         auto ress = find(ia+0,ia+5,75);

         cout<<*ress<<endl;

         //find over

         //count函数统计序列中给定值的出现次数,vector list

         auto cou = count(vec.begin(),vec.end(),65);

         cout<<cou<<endl;

         list<string> str{"sd","xf","df","vf","df","kf","gf","ef","df","cf","df","af"};

         auto coup = count(str.begin(),str.end(),"df");

         cout<<coup<<endl;

         return 0;

    }

     二、accumulate 求和相加,equal对比序列,fill 容器元素重置,重新赋值

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    #include <numeric>

    using namespace std;

    int main(int argc, char**argv)

    {

        //accumulate 求和函数,0是作为求和基数

         vector<int> vec{23,456,654,7,65,89,65,89,65,98};

         int sum = accumulate(vec.cbegin(),vec.cend(),0);

         cout<<sum<<endl;

         vector<string> vecs{"da","fr","ws","vd","bg"};//对于字符串相加

        // string sums = accumulate(vecs.cbegin(),vecs.cend()," ");//错误写法

         string sums = accumulate(vecs.cbegin(),vecs.cend(),string("sums = "));

         cout<<sums<<endl;

         //equal序列对比函数,比较元素是否一致

          vector<int> vec1{23,456,654,7,65,89,65,89,65,98,98};

          bool res = equal(vec.cbegin(),vec.cend(),vec1.cbegin());

          cout<<res<<endl;

          //fill 容器快速赋值

          vector<int> vec2{22,44,556,656};

          //fill(vec2.begin(),vec2.end(),10);//全部重置

          fill(vec2.begin(),vec2.begin() + 2,10);//部分重置<br><br>    fill_n(vec2.begin(),2,20);//部分修改

          for(auto iter = vec2.begin();iter!=vec2.end();iter++)

          {

              cout<<*iter<<endl;

          }

         return 0;

    }

     三、 copy拷贝,replace ,replace_copy替换函数

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    using namespace std;

    int main(int argc, char**argv)

    {

         //copy 算法

         int a1[]={0,1,2,3,4,5};

         int a2[sizeof(a1)/ sizeof(*a1)];//大小一致

         auto ret = copy(begin(a1),end(a1),a2);//拷贝a1给a2,ret指向a2尾后元素

         //cout<<*(ret-1)<<endl;

         for(auto iter = begin(a2);iter!=end(a2);iter++)

         {

            // cout<<*iter<<endl;

         }

          

         //replace 元素替换函数

         list<int> vec{2,4,6,8,9,0,3};

         replace(vec.begin(),vec.end(),0,20);//找0替换成20

         //拷贝替换,不改变原vector

         list<int> vec2;

         replace_copy(vec.cbegin(),vec.cend(),back_inserter(vec2),0,20);// 拷贝并替换

         for(auto iter = vec2.begin();iter!=vec2.end();iter++)

         {

             cout<<*iter<<endl;

         }

         return 0;

    }

  • 相关阅读:
    JS保留两位小数
    xml,json,html格式化工具
    SQL查询递归
    javascript验证价格输入
    类库说明文档生成器
    转换数据库时间字段格式函数
    mysql python 汉字乱码的解决方案
    Python list 按元素属性排序, key 的用法
    原以为会不同的
    libsvm 文本分类(分两类)(实验结果)直接调用Libsvm工具
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13309177.html
Copyright © 2011-2022 走看看