arham___khan's blog

By arham___khan, history, 2 months ago, In English

If I have a map<string,pair<int,int>> and I want to sort it in three different ways. 1.Sort by key. 2.Sort by value of first element in the pair. 3.Sort by value of second element in the pair.

Example : if mp [ {a,{9,7}} {y,{2,5}} {g,{3,1}} {i,{6,6}} {e,{1,2}} ]

1.Sort by key:
2.Sort by value of first element in the pair:
3.Sort by value of second element in the pair:

How to implement the above-mentioned three ways of sorting?

 
 
 
 
  • Vote: I like it
  • -9
  • Vote: I do not like it

»
2 months ago, # |
  Vote: I like it +9 Vote: I do not like it

Elements in the map are sorted by default by the key.

you can't change its order

  • »
    »
    2 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Then how do I sort the list?

    • »
      »
      »
      2 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      use a vector<pair<string,pair<int,int>>>

      • »
        »
        »
        »
        2 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Using vector<tuple<string, int, int>> is working fine for me with custom comparator. Thanks for the help though

»
2 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I think you are writing this post after thinking implementation of Div3 B2 Problem. Am I right ? Yes I am also trying to get the same answer I will be following this post to get those answer. : — )

»
2 months ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

Hello! I would recommend you implementing by this way, by vector of pairs and comparators. The link: https://pastebin.com/f2bUv82W. I hope that I helped you.

»
2 months ago, # |
  Vote: I like it 0 Vote: I do not like it

hey can someone help me with this problem? https://codeforces.com/contest/143/problem/A this is my solution https://pastebin.com/ViwFKvWP

  • »
    »
    2 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    You should make your own post describing in short:

    • what have you learned from editorial to this contest and other participants solutions
    • why it doesn't suit you
    • what is your approach and what's your problem in this problem
»
2 months ago, # |
  Vote: I like it +3 Vote: I do not like it

Instead of having this convoluted of a data structure, try to come up with a better way of implementing all around.

»
2 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Check out my solution . its simple than u r thinking, MY IMPLEMENTATION for problem 1551 B2 any suggestions to make my solution cleaner are appreciated