这个表达式为什么要这么写?

wanren13 2014-04-07 08:04:51
在网上看到c++ min函数的实现,如下:
template <class T> const T& min (const T& a, const T& b) {
return !(b<a)?a:b; // or: return !comp(b,a)?a:b; for version (2)
}


为什么要用“!“?注释里的写法也用了”!“。为什么不直接写成如下:
template <class T> const T& min (const T& a, const T& b) {
return a<b?a:b; // or: return !comp(b,a)?a:b; for version (2)
}


有什么玄机吗?
...全文
180 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
JerryWang365 2014-04-11
  • 打赏
  • 举报
回复

楼主您好!!我想请问上图中的C/C++使用什么软件写的!谢谢您!我是个初学者!
AntiMoron 2014-04-09
  • 打赏
  • 举报
回复
!(b<a) 不就是 a>=b么 = = 有什么好纠结的 一点玄机也没有。 完全凭实现者小学数学怎么学的。
lming_08 2014-04-09
  • 打赏
  • 举报
回复
引用 4 楼 buyong 的回复:
The versions for initializer lists (3) return the smallest of all the elements in the list. Returning the first of them if these are more than one. 看到玄机了吧
意思是说该函数的功能规定了返回第一个,而若没有此规定,那么楼主的第二种写法应该也是可以的吧
wanren13 2014-04-08
  • 打赏
  • 举报
回复
引用 4 楼 buyong 的回复:
The versions for initializer lists (3) return the smallest of all the elements in the list. Returning the first of them if these are more than one. 看到玄机了吧
这个函数和intiailier lists有什么关联?该如何使用?
qq120848369 2014-04-07
  • 打赏
  • 举报
回复
引用 4 楼 buyong 的回复:
The versions for initializer lists (3) return the smallest of all the elements in the list. Returning the first of them if these are more than one. 看到玄机了吧
学习了。
buyong 2014-04-07
  • 打赏
  • 举报
回复
The versions for initializer lists (3) return the smallest of all the elements in the list. Returning the first of them if these are more than one. 看到玄机了吧
ithiker 2014-04-07
  • 打赏
  • 举报
回复
涉及到规则、范式等逻辑推到方面的内容 b<a不成立需要能够导出a<b,这里给出的其实是a<=b
buyong 2014-04-07
  • 打赏
  • 举报
回复
引用 1 楼 XWL1992 的回复:
因为comp(b,a)返回有三种情况1,0,-1,这里把-1, 0 要看做一种情况,即b不小于a,
没道理,返回0表示相等,这种情况返回哪个都行
us天道酬勤 2014-04-07
  • 打赏
  • 举报
回复
因为comp(b,a)返回有三种情况1,0,-1,这里把-1, 0 要看做一种情况,即b不小于a,
幻夢之葉 2014-04-07
  • 打赏
  • 举报
回复
这思想真是可以,又避免某些不支持=比较的类型?
titer1 2014-04-07
  • 打赏
  • 举报
回复
一句话,no more than

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧