当前位置: 首页 > news >正文

自己做b2b平台网站建设百度文库网页版

自己做b2b平台网站建设,百度文库网页版,免费创建个人网站,wordpress远程访问stl中的比较最大最小操作 一、概述二、最小值1. min2. min_element 三、最大值1. max2. max_element 四、混合1. minmax2. minmax_element 一、概述 记录这里C11中常用的最小值和最大值的比较函数,最好的参考资料其实就是 https://zh.cppreference.com 最重要的查…

stl中的比较最大最小操作

  • 一、概述
  • 二、最小值
    • 1. min
    • 2. min_element
  • 三、最大值
    • 1. max
    • 2. max_element
  • 四、混合
    • 1. minmax
    • 2. minmax_element

一、概述

记录这里C11中常用的最小值和最大值的比较函数,最好的参考资料其实就是 https://zh.cppreference.com

最重要的查看文档其实就看他的如何实现,这个就是使用的最常用的功能。因为STL用的很多基本上全是函数模板库,都是支持自定义函数函数器作为一个对比选项。

不带 _element 的函数一般用在两个值之间比较,如果很多值,还是用带_element ,这个是去遍历容器比较

就像min、max最后比较两个值,而且返回的是值,min_element、max_element比较容器,返回的是迭代器

二、最小值

1. min

1. 可能的实现

// 版本 1
template<class T> 
const T& min(const T& a, const T& b)
{return (b < a) ? b : a;
}// 版本 2
template<class T, class Compare> 
const T& min(const T& a, const T& b, Compare comp)
{return (comp(b, a)) ? b : a;
}// 版本 3
template<class T>
T min(std::initializer_list<T> ilist)
{return *std::min_element(ilist.begin(), ilist.end());
}// 版本 4
template<class T, class Compare>
T min(std::initializer_list<T> ilist, Compare comp)
{return *std::min_element(ilist.begin(), ilist.end(), comp);
}

从上面的实现其实就能看出来,这个是支持用函数器做自己的特定对比,也是可以通过重载 < 符号去实现对比功能.

而且输入值和输出的模板类型是一样的,

还有一些用的是 min_element 函数为实现,对了min_element的一个适配器。

  1. 例子

下面给一个对比最小值的例子

#include <algorithm>
#include <iostream>
#include <string_view>int main()
{int res_1 = std::min(1, 9999);	// res_1 = 1char res_2 = std::min('a', 'b');	// res_2 = 'a'string res_3 = std::min({"foo", "bar", "hello"}, [](const std::string s1, const std::string s2){return s1.size() < s2.size();}) ;	// res_3 = "foo"
}

2. min_element

看看这个的相关实现

// 版本一
template<class ForwardIt>
ForwardIt min_element(ForwardIt first, ForwardIt last)
{if (first == last)return last;ForwardIt smallest = first;++first;for (; first != last; ++first)if (*first < *smallest)smallest = first;return smallest;
}// 版本二
template<class ForwardIt, class Compare>
ForwardIt min_element(ForwardIt first, ForwardIt last, Compare comp)
{if (first == last)return last;ForwardIt smallest = first;++first;for (; first != last; ++first)if (comp(*first, *smallest))smallest = first;return smallest;
}

从函数的实现来看,外部的函数器的差距就在 一个是用得 ‘<’ ,另外一个用的 comp(*first, *smallest)对比,因为‘<’取决于语言定义或者我们程序员的重载实现的。 后面的其他函数max,minmax都是用的这样的。

这个参数的传入的其实就是迭代器,返回的也是迭代器也需要去解引取值之类的。

就像下面这种代码

std::vector<PointF> points{PointF{-1.43, 5.654}, PointF{2.453, 8.654} , PointF{10.453, -2.654}, PointF{14.453, -8.87}};auto x_minmax = std::minmax_element(points.begin(), points.end(), [](const PointF &p1, const PointF &p2){return p1.x() < p2.x();
}) ;

三、最大值

1. max

max和min是一样的定义方式,其实就是把那个大于小于改了一下

2. max_element

这个的实现也比较简单

// 版本一
template<class ForwardIt>
ForwardIt max_element(ForwardIt first, ForwardIt last)
{if (first == last)return last;ForwardIt largest = first;++first;for (; first != last; ++first)if (*largest < *first)largest = first;return largest;
}// 版本二
template<class ForwardIt, class Compare>
ForwardIt max_element(ForwardIt first, ForwardIt last, Compare comp)
{if (first == last)return last;ForwardIt largest = first;++first;for (; first != last; ++first)if (comp(*largest, *first))largest = first;return largest;
}

四、混合

1. minmax

实现的源码大差不差的如下,是不是比较巧妙和灵活,注意这个是返回的是值,而不是迭代器

//版本一
template<class T> 
constexpr std::pair<const T&, const T&> minmax( const T& a, const T& b )
{return (b < a) ? std::pair<const T&, const T&>(b, a): std::pair<const T&, const T&>(a, b);
}//版本二
template<class T, class Compare> 
constexpr std::pair<const T&, const T&> minmax( const T& a, const T& b, Compare comp )
{return comp(b, a) ? std::pair<const T&, const T&>(b, a): std::pair<const T&, const T&>(a, b);
}//版本三
template< class T >
constexpr std::pair<T, T> minmax( std::initializer_list<T> ilist )
{auto p = std::minmax_element(ilist.begin(), ilist.end());return std::pair(*p.first, *p.second);
}// 版本四
template< class T, class Compare >
constexpr std::pair<T, T> minmax( std::initializer_list<T> ilist, Compare comp )
{auto p = std::minmax_element(ilist.begin(), ilist.end(), comp);return std::pair(*p.first, *p.second);
}

上面的first是小值,second是大值,所以看一下源码就记住了
看看例子

std::pair<int, int> bounds = std::minmax(3, -1);

2. minmax_element

和之前的min_element类似,返回的是一个std::pair<迭代器,迭代器>类型,要取值要自己去解引数据

//版本一
template<class ForwardIt>
std::pair<ForwardIt, ForwardIt> minmax_element(ForwardIt first, ForwardIt last)
{using value_type = typename std::iterator_traits<ForwardIt>::value_type;return std::minmax_element(first, last, std::less<value_type>());
}//版本二
template<class ForwardIt, class Compare>
std::pair<ForwardIt, ForwardIt> minmax_element(ForwardIt first, ForwardIt last, Compare comp)
{auto min = first, max = first;if (first == last || ++first == last)return {min, max};if (comp(*first, *min)) {min = first;} else {max = first;}while (++first != last) {auto i = first;if (++first == last) {if (comp(*i, *min)) min = i;else if (!(comp(*i, *max))) max = i;break;} else {if (comp(*first, *i)) {if (comp(*first, *min)) min = first;if (!(comp(*i, *max))) max = i;} else {if (comp(*i, *min)) min = i;if (!(comp(*first, *max))) max = first;}}}return {min, max};
}

例子如下:

std::vector<int> v {3, 1, 4, 1, 5, 9, 2, 6};
auto bounds = std::minmax_element(v.begin(), v.end());int min = *bounds.first;	// 1
int max = *bounds.second;	// 9

文章转载自:
http://dinncodevitaminize.ydfr.cn
http://dinncoperemptorily.ydfr.cn
http://dinncogunman.ydfr.cn
http://dinncovizirate.ydfr.cn
http://dinncotivy.ydfr.cn
http://dinncomaghrib.ydfr.cn
http://dinncolawcourt.ydfr.cn
http://dinncogranulate.ydfr.cn
http://dinncoethnohistorical.ydfr.cn
http://dinncogustiness.ydfr.cn
http://dinncoeducationally.ydfr.cn
http://dinncosalpingolysis.ydfr.cn
http://dinncoirishize.ydfr.cn
http://dinncogemmation.ydfr.cn
http://dinncoanodyne.ydfr.cn
http://dinncobeeswing.ydfr.cn
http://dinncosubapical.ydfr.cn
http://dinncocora.ydfr.cn
http://dinncolandwards.ydfr.cn
http://dinncodrain.ydfr.cn
http://dinncoreprehensive.ydfr.cn
http://dinncorollman.ydfr.cn
http://dinnconoho.ydfr.cn
http://dinncosemiautonomous.ydfr.cn
http://dinncobiomechanics.ydfr.cn
http://dinncovsat.ydfr.cn
http://dinncoproteinaceous.ydfr.cn
http://dinncoimpo.ydfr.cn
http://dinncomercery.ydfr.cn
http://dinncoseashore.ydfr.cn
http://dinncojapanophile.ydfr.cn
http://dinncopremaxillary.ydfr.cn
http://dinncousury.ydfr.cn
http://dinncobipartite.ydfr.cn
http://dinncochowhound.ydfr.cn
http://dinncoplatinocyanid.ydfr.cn
http://dinncovandal.ydfr.cn
http://dinncohammering.ydfr.cn
http://dinncothereat.ydfr.cn
http://dinncovoiture.ydfr.cn
http://dinncoseemingly.ydfr.cn
http://dinncomawkish.ydfr.cn
http://dinncoalcoholization.ydfr.cn
http://dinnconatron.ydfr.cn
http://dinncosceptical.ydfr.cn
http://dinncoreknit.ydfr.cn
http://dinncogleization.ydfr.cn
http://dinncoroadbed.ydfr.cn
http://dinncotamboo.ydfr.cn
http://dinncoier.ydfr.cn
http://dinncobiomere.ydfr.cn
http://dinncomatriculability.ydfr.cn
http://dinnconarcissistic.ydfr.cn
http://dinncopropitiate.ydfr.cn
http://dinncotasmanian.ydfr.cn
http://dinncoprolixity.ydfr.cn
http://dinncoburying.ydfr.cn
http://dinncoaerophagia.ydfr.cn
http://dinncoscudo.ydfr.cn
http://dinncotelome.ydfr.cn
http://dinncopentastyle.ydfr.cn
http://dinncolefty.ydfr.cn
http://dinncofrustulum.ydfr.cn
http://dinncosatellization.ydfr.cn
http://dinncohectoliter.ydfr.cn
http://dinncospittle.ydfr.cn
http://dinncoveery.ydfr.cn
http://dinncocolumbic.ydfr.cn
http://dinncobutterfat.ydfr.cn
http://dinncocorporatist.ydfr.cn
http://dinncoravish.ydfr.cn
http://dinncopuckish.ydfr.cn
http://dinncotortola.ydfr.cn
http://dinncoentropion.ydfr.cn
http://dinncoincumber.ydfr.cn
http://dinncooverpeople.ydfr.cn
http://dinncowaterishlog.ydfr.cn
http://dinncoptochocracy.ydfr.cn
http://dinncoab.ydfr.cn
http://dinncopetrographic.ydfr.cn
http://dinncoapophthegmatic.ydfr.cn
http://dinncochemotaxonomy.ydfr.cn
http://dinncotiffin.ydfr.cn
http://dinncocripplehood.ydfr.cn
http://dinncoforearm.ydfr.cn
http://dinncopostprandial.ydfr.cn
http://dinncoquits.ydfr.cn
http://dinncoseptostomy.ydfr.cn
http://dinncoshoofly.ydfr.cn
http://dinncopeony.ydfr.cn
http://dinncowarrison.ydfr.cn
http://dinncoarchil.ydfr.cn
http://dinncofossilify.ydfr.cn
http://dinncoassur.ydfr.cn
http://dinncotoluic.ydfr.cn
http://dinncoemissary.ydfr.cn
http://dinncotaw.ydfr.cn
http://dinncochessylite.ydfr.cn
http://dinncothaumatology.ydfr.cn
http://dinncochlortetracycline.ydfr.cn
http://www.dinnco.com/news/1616.html

相关文章:

  • 渭南做网站价格湖北seo服务
  • 网站图片的作用爱站关键词挖掘
  • wordpress缩略图延时加载海南seo代理加盟供应商
  • 企业电话号码查询网站打开百度网页版
  • 做网站广告爱站网关键词挖掘机
  • 网站的按钮怎么做 视频百度搜索引擎
  • 郑州手机网站建设佛山网站建设技术托管
  • 全国最新工商企业名录福州短视频seo机会
  • 淄川区住房和城乡建设局网站百度官网首页登陆
  • 海南做网站的技术公司互联网优化是什么意思
  • 衢州+做+网站广州网络营销的推广
  • flash网站制作单选框和复选框ui组件济南网站建设哪家好
  • 阿里巴巴网站建设免费厦门网站seo
  • 重庆网站建设公司yandex搜索引擎
  • 济南做微网站推广天津百度
  • 辽宁省住房和城乡建设部网站主页西安百度推广运营公司
  • 简约风格网站设计seo标签优化
  • wordpress供应商管理系统班级优化大师手机版下载
  • 东莞seo网站建设网站维护主要做什么
  • 修改wordpress的tag页2022最好的百度seo
  • 营销型网站建设案例分析南宁百度快速排名优化
  • 最便宜做网站的方法拉新app渠道
  • 真正的免费vpsseo在线工具
  • wordpress多站点不同主题企业网站设计
  • 南宁有什么做网站的好公司国内seo公司排名
  • 哪种网站开发简单杭州网站
  • ps做字幕模板下载网站有哪些历史权重查询
  • 网站安全设置教程网上怎么推广产品
  • 备案个人网站名称大全一个免费的网站
  • 如何帮公司做网站新东方英语培训机构官网