【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);
}
执行结果
**********************技术交流请 email:cuihao0532#163.com 欢迎转载,转载请注明出处!***************************** 如果对本文满意请扫描文章左侧【二维码添加微信】获取更多好玩、有趣、有益、有营养的料,
你我共同成长!Y(^_^)Y
标签:
STL
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗