0

I want to sort a std::map based on the data (second field). However the second field itself is a structure and I want to sort based on one of its elements. Based on what is suggested here and its reference, decided to copy the map to a vector and then use std::sort. Here is the class implementtion

#include <iostream>
#include <vector>
#include <map>
#include <utility>

class foo() {
    foo() {}
    void bar()
    {
      aDeltaMap theDeltaMap;
      // insert some elements to theDeltaMap
      aDeltaVector theDeltaVec( theDeltaMap.begin(), theDeltaMap.end() );
      std::sort(theDeltaVec.begin(), theDeltaVec.end(), descend_rep<deltaPair>() );   //ERROR
    }
private:
    typedef struct entry {
      entry( int r, int mc ) : rep(r), missCounter(mc) {}
      int rep;
      int missCounter;
    } aDeltaEntry;

    typedef std::map< int, aDeltaEntry > aDeltaMap;
    typedef std::pair< int, aDeltaEntry > deltaPair;
    typedef std::vector< deltaPair > aDeltaVector;
    struct descend_rep
      : std::binary_function<deltaPair,deltaPair,bool>
      {
         inline bool operator()(const deltaPair& lhs, const deltaPair& rhs) { return lhs.second.rep > rhs.second.rep; };
      };
 };

At the line of sort function, I get this error

 error C2275: illegal use of this type as an expression
 error C2059: syntax error : ')'

What did I missed?

0

1 Answer 1

1

One error is that descent_rep is not a class template, so you need to replace

descend_rep<deltaPair>()

by

descend_rep()

You should make descend_rep's bool operator() const too, since comparing its operands does not change its state.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I thought it is not necessary to write a template because I have only one data type. Then I forgot to remove the template

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.