網站首頁 編程語言 正文
1.for_each
功能描述
實現容器遍歷
函數原型
for_each(itertor beg,iterator end,_func);
//遍歷算法 遍歷容器元素
//beg 開始迭代器
//end 結束迭代器
//_func函數或者函數對象
代碼
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
//普通函數
void print01 (int val)
{
cout<< val << " ";
}
//放寒暑
class print02
{
public:
void operator()(int val)
{
cout<< val << " ";
}
};
void test01()
{
vector<int>v;
for(int i = 0;i < 10;i ++)
{
v.push_back(i);
}
for_each(v.begin(),v.end(),print01);
cout<<endl;
for_each(v.begin(),v.end(),print02());
cout<<endl;
}
int main()
{
test01();
}
運行結果
2.transform
功能描述
搬運容器到另一個容器
函數原型
transform (iterator beg1,iterator endl, iterator beg2,_func);
//beg1 源容器開始迭代器
//end1 源容器結束迭代器
//beg2 目標函數開始迭代器
//_func 函數或者函數對象
代碼?
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
class Transform
{
public:
int operator()(int v)
{
return v;
}
};
class MyPrint{
public:
void operator()(int val)
{
cout<< val <<" ";
}
};
void test01()
{
vector<int>v;
for(int i = 0;i < 10;i ++)
{
v.push_back(i);
}
vector<int>vTarget; //目標函數
vTarget.resize(v.size()); //目標容器要提前開辟空間
transform(v.begin(), v.end(), vTarget.begin(),Transform());
for_each(vTarget.begin(),vTarget.end(), MyPrint());
cout<<endl;
}
int main()
{
test01();
}
運行結果
原文鏈接:https://ywy666.blog.csdn.net/article/details/128194117
相關推薦
- 2022-06-01 C#條件編譯、內聯函數、CLS介紹_C#教程
- 2022-06-27 詳解Python中while無限迭代循環方法_python
- 2022-07-29 pytest?fixtures函數及測試函數的參數化解讀_python
- 2022-06-25 C#將DataGridView中的數據保存到CSV和Excel中_C#教程
- 2022-07-29 C#?中的多態底層虛方法調用詳情_C#教程
- 2022-08-12 Android學習之BottomSheetDialog組件的使用_Android
- 2022-09-26 Ubuntu 22.04 源碼安裝 openssl 1.1 error while loading
- 2022-11-05 Swift運算符使用方法淺析_Swift
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支