博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
std::nth_elelment排序
阅读量:4158 次
发布时间:2019-05-26

本文共 2244 字,大约阅读时间需要 7 分钟。


本文转自:
[cpp]
  1. template<class _RanIt, class _Pr> inline  
  2.     void nth_element(_RanIt _First, _RanIt _Nth, _RanIt _Last, _Pr _Pred)  
  3.   
  4. template<class _RanIt> inline  
  5.     void nth_element(_RanIt _First, _RanIt _Nth, _RanIt _Last)  
template
inline void nth_element(_RanIt _First, _RanIt _Nth, _RanIt _Last, _Pr _Pred)template
inline void nth_element(_RanIt _First, _RanIt _Nth, _RanIt _Last)

该函数的作用为将迭代器指向的从_First 到 _last 之间的元素进行二分排序,以_Nth 为分界,前面都比 _Nth 小(大),后面都比之大(小);但是两段内并不有序。

特别适用于找出前k个最大(最小)的元素。

例如:

[cpp]
  1. vector<int> temp;  
  2. //  
  3. // calc the value of temp;  
  4. //  
  5.   
  6. nth_element(temp.begin(), temp.begin()+10,temp.end());  
  7.   
  8. //   
vector
temp;//// calc the value of temp;//nth_element(temp.begin(), temp.begin()+10,temp.end());//
如今遇到一个问题:要对结构体按照自定义的方式进行排序,那么可以用到第一个方法了。

首先定义一个返回bool类型的比较函数,然后将函数名作为参数传入。

例如:现要求按照价格挑出最贵的10本书。

[cpp]
  1. bool prizeCompare(const CBook &b1, const CBook &b2)  
  2. {  
  3.       return b1.prize>b2.prize;  
  4. }  
  5.   
  6. // ......  
  7.   
  8. vector<CBook> books;  
  9.   
  10. //挑出最贵的10本书  
  11.   
  12. std::nth_element(books.begin(), books.begin()+10,books.end(), prizeCompare);  
  13. books.resize(10);  
  14.     
bool prizeCompare(const CBook &b1, const CBook &b2){      return b1.prize>b2.prize;}// ......vector
books;//挑出最贵的10本书std::nth_element(books.begin(), books.begin()+10,books.end(), prizeCompare);books.resize(10);

更深一步,如今只对物理学的书进行排序,现有一个vector<int> physics 只记录了书在books中的序号。要求挑出10本最贵的物理学的书。

代码如下:

[cpp]
  1. struct Cmp{  
  2.     std::vector<Book> _list;  
  3.     Cmp(const std::vector<Book> &_books):_list(_books){}  
  4.     bool operator()(int idx1,int idx2)  
  5.     {  
  6.           return _list[idx1].prize>_list[idx2].prize;  
  7.      }  
  8.        
  9.   }     
  10. //......  
  11.   
  12. vector<CBook> books;  
  13. vector<int> physics;  
  14.   
  15. for(int i=0;i<books.size();i++){ if(books[i].category == PHYSICS) physics.push_back(i);  
  16. }  
  17.   
  18. //......  
  19.   
  20. std::nth_element(physics.begin(), physics.begin()+10,physics.end(), Cmp(books));  
  21. physics.resize(10);  
struct Cmp{    std::vector
_list; Cmp(const std::vector
&_books):_list(_books){} bool operator()(int idx1,int idx2) { return _list[idx1].prize>_list[idx2].prize; } } //......vector
books;vector
physics;for(int i=0;i

[cpp]
  1. <pre class="cpp" name="code"><pre></pre>  
  2. <pre></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
  5. <pre></pre>  
  6. <pre></pre>  
  7. <pre></pre>  
  8. <pre></pre>  
  9. </pre>  

转载地址:http://hvuxi.baihongyu.com/

你可能感兴趣的文章
利用Simple-RTMP-Server(SRS)来进行直播
查看>>
live555 h264 videostream 数据流和时间戳的分析
查看>>
h264 NALU的获取与分析
查看>>
Linux的FTP服务端软件(stupid-ftpd)
查看>>
python3.6通过urllib模块使用post/get方法
查看>>
Mp4v2实现h264+aac打包成Mp4视频文件
查看>>
解决linux下不生成core dump文件
查看>>
linux c开发: 在程序退出时进行处理
查看>>
RTMP协议握手详解
查看>>
嵌入式linux交叉编译jrtplib库
查看>>
H264--NALU/SPS/PPS
查看>>
RTP报文头中的SSRC和CSRC
查看>>
eXosip示例程序——注册/认证 .
查看>>
一个简单的eXosip的register注册例子
查看>>
超级详细Tcpdump 的用法
查看>>
理解TCP长连接(Keepalive)
查看>>
eXosip事件总结
查看>>
sip与sdp
查看>>
du -h -d1 只查看一级目录统计的空间占用
查看>>
linux中的strip命令简介
查看>>