zoukankan      html  css  js  c++  java
  • 【STL】帮你复习STL泛型算法 一

    STL泛型算法

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    #include <numeric>
    #include  <list>
    using std::cout;
    using std::endl;
    using std::vector;
    using std::list;
    
    
    
    bool  IsOushu(const int& nNum);
    bool IsBigger(const int& nFirst, const int& nSecond);
    
    int main()
    {
        vector<int> iVec;
        for(int i = 0; i < 10; ++ i)
            iVec.push_back(i);
    
        cout << endl;
        typedef vector<int> IVEC;
    
        //std::find
        IVEC::const_iterator iter = std::find(iVec.begin(), iVec.end(), 5);
        if(iVec.end() != iter)
            cout << endl << "The value is " << *iter << endl;
        else
            cout << endl << "Can not find the value " << 5 << endl;
    
    
        //std::accumulate
        int nSum = std::accumulate(iVec.begin(), iVec.end(), 100);
        cout << endl << "The sum is " << nSum << endl;
    
        cout << endl;
    
        //fill
        vector<int> iVec2(20);
        std::fill(iVec2.begin(), iVec2.end(), 100);
        for(IVEC::const_iterator iter = iVec2.begin(); iter != iVec2.end(); ++ iter)
            cout << *iter << ", ";
    
        cout << endl;
    
        //fill_n
        vector<int> iVec3(5);
        std::fill_n(back_inserter(iVec3), 10, 100);
        cout << endl << "size of iVec3 is " << iVec3.size() << endl;
        for(IVEC::const_iterator iter = iVec3.begin(); iter != iVec3.end(); ++ iter)
            cout << *iter << ", ";
        cout << endl;
    
        cout << endl;
    
        //copy
        vector<int> iVec4;
        list<int> lst1;
        for(int i = 0; i < 10; ++ i)
            lst1.push_back(i);
    
        std::copy(lst1.begin(), lst1.end(), back_inserter(iVec4)); 
        for(IVEC::const_iterator iter = iVec4.begin(); iter != iVec4.end(); ++ iter)
            cout << *iter << ", ";
        cout << endl << endl;
    
        //copy
        vector<int> iVec5(11);
        std::copy(lst1.begin(), lst1.end(), iVec5.begin());
        for(IVEC::const_iterator iter = iVec5.begin(); iter != iVec5.end(); ++ iter)
            cout << *iter << ", ";
        cout << endl << endl;
        
        //replace
        list<int> lst2;
        for(int i = 0; i < 10; ++ i)
            lst2.push_back(i * 2);
        cout << endl;
    
        //打印replace之前到值
        cout << endl << "打印lst2 replace之前到值 " << endl;
        for(list<int>::const_iterator iter = lst2.begin(); iter != lst2.end(); ++ iter)
            cout << *iter << ", ";
        cout << endl;
        cout << "打印replace之后到值 " << endl;
        std::replace(lst2.begin(), lst2.end(), 8, 888);
        for(list<int>::const_iterator iter = lst2.begin(); iter != lst2.end(); ++ iter)
            cout << *iter << ", ";
        cout << endl;
    
    
       cout << endl;
       //replace_copy
       list<int> lst3(lst2.size());
       std::replace_copy(lst2.begin(), lst2.end(), lst3.begin(), 888, 999);
       cout << endl << "打印lst2 replace_copy 之后 lst3 到值 " << endl;
       for(list<int>::const_iterator iter = lst3.begin(); iter != lst3.end(); ++ iter)
             cout << *iter << ", ";
       cout << endl;
    
       //stable_sort
       vector<int> iVec6;
       for(int i = 0; i < 10; ++ i)
          iVec6.push_back(i);
       cout << endl;
       cout << endl << "打印stable_sort之前到iVec6到值 " << endl;
       for(IVEC::const_iterator iter = iVec6.begin(); iter != iVec6.end(); ++ iter)
          cout << *iter << ", ";
       cout << endl << "打印stable_sort之后到iVec6到值 " << endl;
      
       std::stable_sort(iVec6.begin(), iVec6.end(), IsBigger);
     
       for(IVEC::const_iterator iter = iVec6.begin(); iter != iVec6.end(); ++ iter)
          cout << *iter << ", ";
       cout << endl << endl;
       
      
       //count_if
       cout << endl << "计算iVec6中偶数到个数 " << endl;
       int nNums = std::count_if(iVec6.begin(), iVec6.end(), IsOushu);
       cout << endl << "iVec6中偶数个数为 " << nNums <<"" << endl;  
      
    
    
        cout << endl << endl;
    
        cout << "
    This is main function 
    ";
        return 0; 
    
    } 
    
    
    //stable_sort 降序排列
    bool IsBigger(const int& nFirst, const int& nSecond)
    {
        return nFirst > nSecond;
    }
      
    
    
    //是偶数
    bool  IsOushu(const int& nNum)
    {
        return (0 == nNum % 2);
    }

    执行结果

  • 相关阅读:
    window.parent ,window.top,window.self 详解及parent和opener的区别
    jQuery $.extend()用法总结
    JQuery中each()的使用方法说明
    jQuery.isPlainObject()的作用
    change和onchange、click和onclick的区别
    zabbix客户端一键安装脚本(主动模式监控)
    jumpserver在centos 7上的部署
    用阿里云的免费 SSL 证书让网站从 HTTP 换成 HTTPS
    GitLab的安装及使用教程
    一个客户端一键安装环境和服务的shell脚本
  • 原文地址:https://www.cnblogs.com/cuish/p/3926010.html
Copyright © 2011-2022 走看看