当前位置:网站首页>可变参数模板实现max(接受多个参数,两种实现方式)
可变参数模板实现max(接受多个参数,两种实现方式)
2022-06-24 10:22:00 【君莫vv】
仿函数实现和递归实现
#include <iostream>
struct __Iter_less_iter
{
template<typename iteraotr1,typename iterator2>
bool operator() (iteraotr1 it1, iterator2 it2)
{
return *it1 < *it2;
}
};
__Iter_less_iter __iter_less_iter()
{
return __Iter_less_iter();
}
template<typename Iterator,typename comp>
Iterator __max_element(Iterator first, Iterator last, comp cmp)
{
if (first == last) {
return first;
}
Iterator reslut = first;
while (++first != last) { //last是指向最后一个元素的下一个位置
if (cmp(reslut, first)) {
reslut = first;
}
}
return reslut;
}
template<typename T>
T max_element(T first,T last)
{
return __max_element(first, last, __iter_less_iter());
}
template <typename T>
T max(std::initializer_list<T> L)
{
return *max_element(L.begin(), L.end());
}
template <typename T>
T maxnum(T& in)
{
return in;
}
template <typename T,typename... Args>
T maxnum(T first, Args... args)
{
return std::max(first, maxnum(args...));
}
int main()
{
std::cout << max({ 23,42,44,22,11 }) << std::endl;
std::cout << maxnum( 23,42,44,22,11 ) << std::endl;
return 0;
}边栏推荐
- H5 video conference, camera monitoring, web streaming and live broadcast integration scheme
- Analysis and understanding of Jieba stutter word segmentation principle HMM application in Chinese word segmentation and partial code reading
- Nacos source code - configure automatic update
- Google Earth engine (GEE) - how to add a legend in the map panel
- [activities this Saturday] NET Day in China
- Cook a delicious cli
- 初识string+简单用法(一)
- @RequestBody注解
- PHP SMS notification + voice broadcast automatic double call
- 如何只导出word文档中的标题?(即将正文内容都删除,只保留标题)B站牛逼
猜你喜欢

美团基于 Flink 的实时数仓平台建设新进展

Any 与 TypeVar,让 IDE 的自动补全更好用

math_ Summation and derivation of proportional series & derivation of sum and difference of equal powers / difference between two nth power numbers/

Libuv的安装及运行使用

TP-LINK 1208路由器教程(2)

MYSQL_ Elaborate on database data types

初识string+简单用法(一)

Today in history: Turing's birth day; The birth of the founder of the Internet; Reddit goes online

PHP SMS notification + voice broadcast automatic double call

《opencv学习笔记》-- 图像的载入和保存
随机推荐
What code did the full stack programmer write this month?
Opencv optical flow prediction and remap remapping function usage
Jetpack Compose 教程之 从一开始就投资于良好的导航框架将帮助您在之后节省大量的迁移工作
【数字IC/FPGA】Booth乘法器
Concise tutorial | making cartoon heat map with PPT - EFP graph?!
Audio knowledge (III) -- MFCCs code implementation
PF_ Ring ZC | high speed traffic processing dpdk alternative
《opencv学习笔记》-- 矩阵归一化 normalize()函数
美团基于 Flink 的实时数仓平台建设新进展
[深度学习][pytorch][原创]crnn在高版本pytorch上训练loss为nan解决办法
Extremenet: target detection through poles, more detailed target area | CVPR 2019
What is the function of the graphics card driver? Do you want to update the graphics card driver
Can text pictures be converted to word? How to extract text from pictures
How to use data analysis tools to deal with emergencies in retail industry
齐次坐标的理解
计组-总复习
《opencv学习笔记》-- 图像的载入和保存
《opencv学习笔记》-- 分离颜色通道、多通道混合
@Requestbody annotation
把腾讯搬到云上,治愈了他们的技术焦虑