STL overloads the map in the sort of

Copyright: private individuals do question summed up ~ https://blog.csdn.net/tb_youth/article/details/89602706

The data 1.map default key value by lexicographically row of
chestnut:

#include <iostream>
#include <map>
using namespace std;
//typedef pair<char,int>PAIR;
int main()
{
    map<char,int>mp;
    mp.insert(make_pair('b',1));//插入数据方式1
    mp['a'] = 2;//插入数据方式2
    mp.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;
}

operation result:
Here Insert Picture Description

#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;
}

Here Insert Picture Description
But if the key is now a very large number (use the string representation) , we hope to make it to sort by ascending numbers, and do not map overloads can do it?
A first look at the following topics:
Topic background
of the universe presidential campaign

Title Description
Earth in calendar year 6036, the whole universe to run for one of the most elite of the people as president, there are n number of extraordinary top-notch people to run for president, the votes have been tallied and now, you can calculate who became president.

Input Output Format
Input Format:
president.in

The first line an integer n, the number of representatives running for president.

Then there are n rows, respectively, the first candidate to n-th number of votes candidates.

Output Format:
president.out

A total of two lines, the first line is an integer m, the number when the number of people on the president.

The second line is to become president of the people's votes.

Input Output Sample
Input Sample # 1:

5
98765
12365
87954
1022356
985678

Output Sample # 1:

4
1022356

Description
votes may be very large, may be up to 100 digits.

n <= 20
this subject I want to do with the map :
without reloading all right?
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;
}

Sample Test Results:
Here Insert Picture Description
The above code example of this can be off, but see the following set of data:

7
6791385765449865851630484098561093867193
6791385765405861305476138956183659819548
6791385765448765481033867082657092835470
6791385765476183659186548165418634013875
6791385765413054861086540816508058173710
6365470813654816508136547081654108365108
36571811836547138541

The test result is incorrect:

Here Insert Picture Description
As can be seen, without overloading can not achieve our goal (key is a long string of numbers, making it small to large order (or descending)).
Starting with a map do not overload on that question, WA. The reason is that I am mistaken lexicographical can be a very long string of numbers from small to large sort.
So I want to map to achieve large numbers from small to large (or descending) ordering how to do? Of course, it is of key reloaded slightly.
Heavy-duty front of key first look at the STL map template:

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

class Compare = less map specified here can be seen as a less default comparison function (object)
so overloaded comparison function :

#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;
}

The result:
Here Insert Picture Description
2. If you want ` the Map values are sorted by value how to do?
For example, with a map to achieve, we want students by achievement ascending row .
Directly on the code:

#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;
}

The result:
Here Insert Picture Description
more details refer to the blog:
https://blog.csdn.net/iicy266/article/details/11906189

Guess you like

Origin blog.csdn.net/tb_youth/article/details/89602706