日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL之map中排序方式的重载

發布時間:2024/9/3 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL之map中排序方式的重载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • map中的數據默認是按key值字典序排
    栗子:

    #include <iostream> #include <map> using namespace std; //typedef pair<char,int>PAIR; int main() {map<char,int>mp;mp.insert(make_pair('b',1));//插入數據方式1mp['a'] = 2;//插入數據方式2mp.insert(make_pair('A',3));for(map<char,int>::iterator it = mp.begin(); it!=mp.end(); it++){cout<<it->first<<" "<<it->second<<endl;}return 0; }
  • 運行結果:

    #include <iostream> #include <string.h> #include <map> using namespace std; int main() {map<string,int>mp;mp.insert(make_pair("bab",1));mp["abb"] = 2;mp.insert(make_pair("Adfd",3));mp["baaa"] = 4;mp["gfgggh"] = 5;for(map<string,int>::iterator it = mp.begin(); it!=mp.end(); it++){cout<<it->first<<" "<<it->second<<endl;}return 0; }


    但是現在如果key值為一個很大的數字(要用字符串表示),我們希望讓它按數字從小到到排序,不對map重載能行嗎?
    下面先來看個題目:
    題目背景
    宇宙總統競選

    題目描述
    地球歷公元6036年,全宇宙準備競選一個最賢能的人當總統,共有n個非凡拔尖的人競選總統,現在票數已經統計完畢,請你算出誰能夠當上總統。

    輸入輸出格式
    輸入格式:
    president.in

    第一行為一個整數n,代表競選總統的人數。

    接下來有n行,分別為第一個候選人到第n個候選人的票數。

    輸出格式:
    president.out

    共兩行,第一行是一個整數m,為當上總統的人的號數。

    第二行是當上總統的人的選票。

    輸入輸出樣例
    輸入樣例#1:

    5 98765 12365 87954 1022356 985678

    輸出樣例#1:

    4 1022356

    說明
    票數可能會很大,可能會到100位數字。

    n<=20
    這個題目我想用map來做
    不重載行嗎?
    WA code:

    #include <iostream> #include <map> #include <string.h> using namespace std; map<string,int>mp; int main() {int n;cin>>n;string s;for(int i = 1; i <= n; i++){cin>>s;mp[s] = i;}map<string,int>::iterator it = mp.begin();cout<<it->second<<'\n'<<it->first<<endl;return 0; }

    樣例測試結果:

    上面這份代碼樣例是能過的,但是看下面這組數據:

    7 6791385765449865851630484098561093867193 6791385765405861305476138956183659819548 6791385765448765481033867082657092835470 6791385765476183659186548165418634013875 6791385765413054861086540816508058173710 6365470813654816508136547081654108365108 36571811836547138541

    測試結果卻是不正確的:


    可以看出,不作重載無法達到我們的目標(key值為一個很長的數字串,讓它從小到大排序(或從大到小))。
    一開始用map沒重載來做上那個問題,WA。原因是我誤認為字典序可以將一個很長的數字串從小到大來排序。
    那么我想讓map來實現大數從小到大(或從大到小)排序該怎么辦?當然就是對key進行重載了。
    對key重載前需要先了解一下STL中map的模板:

    template < class Key, class T, class Compare = less<Key>, class Allocator = allocator<pair<const Key,T> > > class map;

    class Compare=less可以看出map這里指定less作為其默認比較函數(對象)
    所以重載比較函數

    #include <iostream> #include <map> #include <string> using namespace std; typedef pair<string,int>PAIR; ostream& operator<<(ostream& out,const PAIR& p){//重載輸出流return out<<p.second<<'\n'<<p.first; } struct cmp{//重載map的key值排序方式bool operator()(const string& x,const string& y){if(x.length()==y.length())//一樣長的話,字符串比較,大的數更大return x > y;return x.length()>y.length();//更長的數字更大} }; map<string,int,cmp>mp; int main() {int n;cin>>n;string s;for(int i = 1; i <= n; i++){cin>>s;mp.insert(make_pair(s,i));//map[s] = i;}map<string,int,cmp>::iterator it = mp.begin();cout<<*it<<endl;return 0; }

    運行結果:

    2. 如果想讓`map按value值排序怎么辦?
    比如用map實現,想讓學生的按成績升序排
    直接上代碼:

    #include <bits/stdc++.h> using namespace std; typedef pair<string,int> PAIR; //比較函數(重載方式一) bool cmp_by_value(const PAIR& lhs, const PAIR& rhs) {return lhs.second < rhs.second; } /* //函數對象(重載方式二) struct CmpByValue {bool operator()(const PAIR& lhs, const PAIR& rhs) {return lhs.second < rhs.second;} }; */ int main() {map<string, int> name_score_map;name_score_map["LiMin"] = 90;name_score_map["ZiLinMi"] = 79;name_score_map["BoB"] = 92;name_score_map.insert(make_pair("Bing",99));name_score_map.insert(make_pair("Albert",86));//把map中元素轉存到vector中vector<PAIR> name_score_vec(name_score_map.begin(), name_score_map.end());//sort(name_score_vec.begin(), name_score_vec.end(), CmpByValue());sort(name_score_vec.begin(), name_score_vec.end(), cmp_by_value);for (int i = 0; i < name_score_vec.size(); ++i) {cout << name_score_vec[i].first<<' '<<name_score_vec[i].second << endl;}return 0; }

    運行結果:

    更詳細的請參考博客:
    https://blog.csdn.net/iicy266/article/details/11906189

    總結

    以上是生活随笔為你收集整理的STL之map中排序方式的重载的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。