0

i wanted to use a hash map to sort a string on the basis of the values the map has.but i just could not find a suitable way..please help me find a way. so here is a c++ code that I wrote please help me how to write it better i want to know how to use std::sort() by passing a data structure for sorting

#include<bits/stdc++.h>
using namespace std;
unordered_map<char,int>m;
bool h(char a,char b)
{
    return m[a]<=m[b];
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        //unordered_map<char,int>m;
        for(int i=1;i<=26;i++)
        {
            char a;
            cin>>a;
            m[a]=i;
        }
        string s;
        cin>>s;
        sort(s.begin(),s.end(),h);
        cout<<s<<endl;
        //m.erase(m.begin(),m.end());
        //cout<<endl<<m.size();
    }
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • your code does not compilable. Please post a [MRE] – bolov Apr 21 '20 at 21:10
  • also what do you mean by "sort a string on the basis of the values the map has"? Please clearly state what you want to solve, what is the expected result and what errors and/or observable behavior you get. – bolov Apr 21 '20 at 21:11
  • What's the idea? Sort some letters based on arbitrary weights which are stored inside a hashmap? You could do a better job presenting the problem. It needs an input, expected output, actual output, and compilable code ... as already mentioned. – Kenny Ostrom Apr 21 '20 at 21:17
  • Didn't any of the answers help? In that case, can you clarify the question? – Ted Lyngmo Apr 27 '20 at 20:48

2 Answers2

1

Your Compare function does not fulfill the strict weak ordering requirement.

return m[a] <= m[b]; should be return m[a] < m[b];

With that change, your program works correctly and sorts the std::string in the order your map holds. If you enter the characters zyxwvutsrqponmlkjihgfedcba your sort will sort the string in reverse alphabetical order.

Suggestions:

  • Read about why you shouldn't include <bits/stdc++.h>. Include the correct headers instead:

    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <unordered_map>
    
  • Try to avoid magic numbers like 26. You can make your loop like this for the same effect:

    for(int i = 0; i <= 'Z'-'A'; i++)
    

    1-26 and 0-25 (as the above loop produces) will have the same effect.

  • Avoid global variables such as m. You can make it local and refer to it in a functor, like a lambda.

  • Read Why is using namespace std; considered bad practice?

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

I'm going to assume that the commended out lines were what you were trying to get working.

#include <unordered_map>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
    cout << "enter number of times" << endl;
    int t;
    cin>>t;
    while(t--)
    {
        unordered_map<char,int>m;
        cout << "enter 26 characters" << endl;
        for(int i=1;i<=26;i++)
        {
            char a;
            cin>>a;
            m[a]=i;
        }
        cout << "enter a string" << endl;
        string s;
        cin>>s;
        sort(s.begin(),s.end(), [&](char a, char b)
        {
            return m[a]<m[b];
        });
        cout<<s<<endl;
        m.erase(m.begin(),m.end());
        cout<<endl<<m.size();
    }
}
Isaac Clancy
  • 409
  • 2
  • 7