【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 << "\nThis is main function \n";
    return 0; 

} 


//stable_sort 降序排列
bool IsBigger(const int& nFirst, const int& nSecond)
{
    return nFirst > nSecond;
}
  


//是偶数
bool  IsOushu(const int& nNum)
{
    return (0 == nNum % 2);
}
复制代码

 

 

 

 

执行结果

 

posted on   崔好好  阅读(377)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示