当前位置:网站首页>可变参数模板实现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;
}边栏推荐
- Go basic series | 4 Environment construction (Supplement) - gomod doubts
- Influence of DEX optimization on arouter lookup path
- [深度学习][pytorch][原创]crnn在高版本pytorch上训练loss为nan解决办法
- Anonymous Messenger: hidden communication of Trojan horse
- AXI低功耗接口
- PPT绘图相关,快捷键,美观度
- Network monitoring: active troubleshooting becomes simple
- About the unsupported instruction set SSE 4.2 of CPU in virtualization
- MYSQL_ Elaborate on database data types
- Why are some old SEO methods still effective?
猜你喜欢
随机推荐
“一次编写,运行各端”,高通重磅发布 AI 软件栈!
Redis
[latest - lightweight cloud servers - hot sales] new lightweight application server optimization scheme, 1-core 2g5m time limit as low as 99 yuan / year
Group policy export import
Suddenly I thought of the wooden house in my hometown
Déplacer Tencent sur le cloud a guéri leur anxiété technologique
System design: key features of distributed systems
A fault record of misoperation dhclient script
How to use arbitrarygen code generator what are the characteristics of this generator
SwiftUI Swift 内功之 Swift 中的属性观察者 didSet 与 willSet
Tencent geek challenge small - endless!
Qt: 判断字符串是否为数字格式
[deep learning][pytorch][original]crnn trains loss on the higher version of pytorch as a solution for Nan
What is wireless WiFi? What are the benefits of wireless WiFi
程序员大部分时间不是写代码,而是。。。
MYSQL_ Elaborate on database data types
d的10个0符
qt -- QTabWidget 中支持拖拽TabBar项
How to improve the quality of Baidu keyword?
Step 3: access the API interface for inquiry of SF express doc No. [express 100api interface]







