2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Map Valueでソート

Last updated at Posted at 2020-01-09

MapのValueでソートする
pair型のvectorに移し替えてソートする。
pairの一つ目でソートされる。


using namespace std;
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
int main()
{
    map<string, int> mp;
    mp["apple"] = 7;
    mp["orange"] = 3;
    mp["melon"] = 5;
    mp["peach"] = 0;
    mp["suica"] = 9;

    vector<pair<int, string>> v;

    for (map<string, int>::iterator it = mp.begin(); it != mp.end(); it++) {
        v.push_back({ it->second, it->first });
    }
    sort(v.begin(), v.end());
    return 0;
}
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?