淺析stl序列容器(map和set)的仿函數(shù)排序
問(wèn)題:set是一個(gè)自動(dòng)有序的集合容器,這是set的一個(gè)最實(shí)惠的性質(zhì),從小到大,只要你插入進(jìn)去,就有序了。但是,如果你不想要這個(gè)順序呢,是不是可以人為控制set容器
的元素順序呢?答案是,可以的,因?yàn)閟tl也是程序員設(shè)計(jì)的。
首先看stl的模板構(gòu)造函數(shù)
explicit set ( const Compare& comp = Compare(), const Allocator& = Allocator() );
template
set ( InputIterator first, InputIterator last, const Compare& comp = Compare(), const Allocator& = Allocator() );
set ( const set& x );
我們完全可以重定義set的構(gòu)造函數(shù)里的比較函數(shù),完成對(duì)set的自排序功能。
舉例:
bool fncomp (int lhs, int rhs) {return lhs
struct classcomp {
bool operator() (const int& lhs, const int& rhs) const
{return lhs>rhs;} // 控制set逆序
};
void testset()
{
// 第一種使用方法
bool(*fn_pt)(int,int) = fncomp;
set sixth (fn_pt);
// 第二中使用方法
set s; // class as Compare
s.insert(4);
s.insert(5);
set::iterator it;
for(it=s.begin();it!=s.end();it++)
{
cout<<*it<<" ";
}
cout <<endl;
};
注意:如果set元素是一個(gè)結(jié)構(gòu)體,你最好要設(shè)置你的仿函數(shù),不然set一般默認(rèn)是按第一個(gè)字段排序的,而我們的實(shí)際情況是想按序號(hào)i排序:
struct ST_Message
{
public:
ST_Message(int seq, int64_t time, string strfrom, string strto, string strinfo){
this->seq=seq;this->time=time;this->strfrom=strfrom;this->strto=strto;this->strinfo=strinfo;}
int seq;
int64_t time;
string strfrom;
string strto;
string strinfo;
bool operator <(const ST_Message& other) const // 注意是const函數(shù)
{
if (seq != other.seq) // dtime按升序排序
{
return (seq < other.seq);
}
else if(time < other.time)
{
return (time < other.time);
}
else if(strcmp(strfrom.c_str(), other.strfrom.c_str()) != 0)
{
return (strcmp(strfrom.c_str(), other.strfrom.c_str()) < 0);
}
else if(strcmp(strto.c_str(), other.strto.c_str()) != 0)
{
return (strcmp(strto.c_str(), other.strto.c_str()) < 0);
}
else
{
return (strcmp(strinfo.c_str(), other.strinfo.c_str()) < 0);
}
}
};
stl中自動(dòng)有序的容器map也和set有相同的應(yīng)用,如果你想快速理解,那么把這篇文章中的set改成map就差不多了。
總之,有序的stl容器在工程中應(yīng)用什么方便和廣泛,但是當(dāng)我們需要自己的排序的時(shí)候,可以用仿函數(shù)來(lái)設(shè)置它!
相關(guān)文章
C++簡(jiǎn)單實(shí)現(xiàn)與分析二叉搜索樹(shù)流程
二叉搜索樹(shù)作為一個(gè)經(jīng)典的數(shù)據(jù)結(jié)構(gòu),具有鏈表的快速插入與刪除的特點(diǎn),同時(shí)查詢(xún)效率也很優(yōu)秀,所以應(yīng)用十分廣泛。本文將詳細(xì)講講二叉搜索樹(shù)的C++實(shí)現(xiàn),需要的可以參考一下2022-08-08
解析C++中多層派生時(shí)的構(gòu)造函數(shù)及一些特殊形式
這篇文章主要介紹了解析C++中多層派生時(shí)的構(gòu)造函數(shù)及一些特殊形式,特殊形式主要針對(duì)基類(lèi)和子對(duì)象類(lèi)型的構(gòu)造函數(shù)內(nèi)容,需要的朋友可以參考下2015-09-09
VC中CWinThread類(lèi)以及和createthread API的區(qū)別分析
這篇文章主要介紹了VC中CWinThread類(lèi)以及和createthread API的區(qū)別分析,較為詳細(xì)的講述了CWinThread類(lèi)的原理,并以實(shí)例形式對(duì)AfxBeginThread函數(shù)的內(nèi)部實(shí)現(xiàn)進(jìn)行了解釋說(shuō)明,需要的朋友可以參考下2014-10-10
C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單翻譯功能
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單翻譯功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
C++實(shí)現(xiàn)LeetCode(692.前K個(gè)高頻詞)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(692.前K個(gè)高頻詞),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
C語(yǔ)言使用realloc函數(shù)實(shí)現(xiàn)通訊錄源碼分析
什么是動(dòng)態(tài)通訊錄,就是在靜態(tài)的基礎(chǔ)上改進(jìn)了一下,不在使用數(shù)組,而是使用指針和動(dòng)態(tài)內(nèi)存開(kāi)辟的函數(shù),當(dāng)空間不夠的時(shí)候,便進(jìn)行增容2023-02-02

