-1
auto cmpli = [](const int &a, const int &b)
    {
        return a > b;
    };

    map<int, set<int, decltype(cmpli)>(cmpli)>ms;
    ms[1].insert(13);
    ms[1].insert(10);
    ms[1].insert(23);
    ms[2].insert(33);
    ms[2].insert(3);

    for (auto s : ms)
    {
        for (auto e : s.second)
            cout << e << " ";
        cout << endl;
    }

// expected output: 23,13,10,33,3

Blockquote How can I make map of set in c++ where elements in sets are sorted in decreasing order?

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207

1 Answers1

0

You're confusing the syntax.

You have probably seen set<int, decltype(cmpli)>> ms(cmpli);, where set<int, decltype(cmpli)>> is the type, ms is the variable name, and (cmpli) is the initializer.

But map<int, set<int, decltype(cmpli)>(cmpli)> ms; doesn't make sense, you can't specify default initializers for elements (or what was it?) like this.

There are several options:

  • You can switch to C++20 or newer and just drop (cmpli). Capture-less lambdas became default-constructible, so everything should just work.

  • Make cmpli a regular function, and replace decltype(cmpli) with std::integral_constant<decltype(&cmpli), cmpli>. Also drop (cmpli).

  • Replace a custom comparator with std::greater: map<int, set<int, greater<>>> ms;. You could also use greater<int>, but greater<> has some useful properties, though they don't matter in this case (look up transparent comparators).

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • Any good resources for these kind of advanced topics? Like I have studied c++ but not taught these topics. – Omprakash kumar Jul 24 '22 at 18:45
  • @Omprakashkumar I've heard good things about [those books](https://stackoverflow.com/q/388242/2752075), but I haven't read those. Ultimately, you have to learn on your own; don't expect teachers or courses to give you much, there are very few good ones. – HolyBlackCat Jul 24 '22 at 18:56
  • Also, my map is map>>m; Can I use greater<> here? if not what are other solution? – Omprakash kumar Jul 24 '22 at 19:02
  • @Omprakashkumar Yes, you can. If you don't specify any comparator, the default one is `less` which calls `<`. And `greater` calls `>`. If a type supports `<`, it most probably supports `>`. – HolyBlackCat Jul 24 '22 at 19:07