dimanche 10 novembre 2019

Sorting a std::map using lambda

I have a map composed by a key (type string) and data (type tuple). I tried to sort my map using a lambda (see code below) but when I compile I got error saying

Severity Code Description Project File Line Suppression State Error C2676 binary '-': 'const std::_Tree_unchecked_iterator>>>' does not define this operator or a conversion to a type acceptable to the predefined operator with [ _Kty=std::string, _Ty=std::tuple ] SandBox C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\algorithm 3466

Severity Code Description Project File Line Suppression State Error C2672 '_Sort_unchecked': no matching overloaded function found SandBox C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\algorithm 3466

My Code:

#include <iostream>
#include <string>
#include <tuple>
#include <map>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;


int main() {
    std::vector<std::string> strarr { "zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"};

    int length = strarr.size();

    std::string str = "";

    map<string, tuple<int, int>> myMap;

    for (int i = 0; i < length; i++)
        myMap[strarr[i]] = make_tuple(strarr[i].length(), ++(get<1>(myMap[strarr[i]])));


    typedef std::function<bool(std::pair<string, tuple< int, int >>, std::pair<string, tuple< int, int >>)> Comparator;

    Comparator compFunctor =
        [](std::pair<string,tuple< int, int>> el1, std::pair<string, tuple< int, int >> el2)
    {
        return (get<0>(el1.second) < get<0>(el2.second));
    };

    std::sort(myMap.begin(), myMap.end(), compFunctor);
}

So what is the error ? I am sure that it is something silly but can't figure it out by my own.

Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire