0

I am getting an error stating 'no match for operator-'. It is happening when I am using the sort() function.

#include <bits/stdc++.h>
using namespace std;

bool comp(pair<char, int> &a, pair<char, int> &b)
{
    return a.first < b.first ? 1 : -1;
}

int main() {
    // your code goes here
    int t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        map<char, int> m;
        for(int i = 0; i < s.size(); i++)
        {
            m[s[i]]++;
            cout << (char)s[i];
        }
        cout << "hello";
        sort(m.begin(), m.end(), comp);
    }
    return 0;
}

image

image

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 6
    Map is by default sorted on key – Sai Sreenivas Aug 04 '20 at 03:34
  • 1
    Your comparison should just be `return a.first < b.first;`. `-1` doesn't make any sense for a `bool`. If you want a map to sort differently you need to specify that at the time you declare it since it is a sorted container. Not sure what you're really trying to do here. – Retired Ninja Aug 04 '20 at 03:35
  • Besides the problems already pointed out, your comparison function should take const references. – eesiraed Aug 04 '20 at 03:52
  • [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Evg Aug 04 '20 at 06:26

1 Answers1

6

std::sort() wants random-access iterators, but std::map iterators are not random-access, so you can't call std::sort() on a std::map, as they don't implement operator-.

std::map is a sorted container, sorted on its keys. And since your keys are simple char, they are already comparable as-is.

The correct way to custom sort a std::map is to either:

  1. provide a comparator directly in the map declaration, eg:
#include <bits/stdc++.h>
using namespace std;

struct comp {
    bool operator()(const char &a, const char &b) const {
        return a < b; // or whatever you want...
    }
};

int main() {
    // your code goes here
    int t;
    cin >> t;
    while (t--) {
        string s;
        cin >> s;
        map<char, int, comp> m;
        for(int i = 0; i < s.size(); i++) {
            m[s[i]]++;
            cout << s[i];
        }
        cout << "hello";
    }
    return 0;
}
  1. Provide an operator< for the key type. You can't overload operators for fundamental types, but you can for custom types:
#include <bits/stdc++.h>
using namespace std;

struct my_key {
    char value;
    my_key(char ch) : value(ch) {}
    bool operator<(const char &rhs) const {
        return value < rhs.value; // or whatever you want...
    }
};

int main() {
    // your code goes here
    int t;
    cin >> t;
    while (t--) {
        string s;
        cin >> s;
        map<my_key, int> m;
        for(int i = 0; i < s.size(); i++) {
            m[s[i]]++;
            cout << s[i];
        }
        cout << "hello";
    }
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770