淺析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)文章
Visual Studio Code 2020安裝教程及CPP環(huán)境配置(教程圖解)
這篇文章主要介紹了Visual Studio Code 2020安裝教程、CPP環(huán)境配置,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Qt無(wú)邊框窗口拖拽和陰影的實(shí)現(xiàn)
自定義窗口控件的無(wú)邊框,窗口事件由于沒(méi)有系統(tǒng)自帶邊框,無(wú)法實(shí)現(xiàn)拖拽拉伸等事件的處理,本文主要介紹了Qt無(wú)邊框窗口拖拽和陰影的實(shí)現(xiàn),感興趣的可以了解一下2024-01-01c++動(dòng)態(tài)庫(kù)調(diào)用的實(shí)現(xiàn)
本文主要介紹了c++動(dòng)態(tài)庫(kù)調(diào)用的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07C/C++寬窄字符轉(zhuǎn)換與輸出的多種實(shí)現(xiàn)方法
本文主要介紹了C/C++寬窄字符轉(zhuǎn)換與輸出的多種實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Cocos2d-x中CCEditBox文本輸入框的使用實(shí)例
這篇文章主要介紹了Cocos2d-x中CCEditBox文本輸入框的使用實(shí)例,本文在代碼中用大量注釋講解了CCEditBox的使用方法,需要的朋友可以參考下2014-09-09C++實(shí)現(xiàn)LeetCode(152.求最大子數(shù)組乘積)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(152.求最大子數(shù)組乘積),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07