2
votes

when I try to insert in this map:

std::map<std::unordered_set<int >, std::pair<float, std::pair<float, float >> >

I got this error

error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::unordered_set,std::equal_to<_Kty>,std::allocator<_Kty>>'

My data is defined as follows:

    struct Trans {
    int Item;
    float Prob;
    float W;
};
bool operator<(const Trans &a, const Trans &b)
    {
        return a.Item < b.Item;
    }
    bool operator==( Trans c,  Trans d) { return c.Item == d.Item; }


    struct MyHash {
        size_t operator()(const Trans& x) const { return std::hash<int>()(x.Item);  }
    };


std::vector<std::vector<Trans>> data;
std::map<std::unordered_set<int>, float> S1;
std::map<std::unordered_set<int >, std::pair<float, std::pair<float, float >> > S2;
std::map<std::unordered_set<int >, std::pair<float, std::pair<float, float >> > S3;

The part that has the problem:

    do
        {

std::unordered_set<Trans, MyHash> KS(data[i].begin(), data[i].begin() + k);
std::unordered_set<int > elem;

float esupp = 1;
float Weight = 0;
float Wesupp = 1;
    for (auto const &iter : KS)
        {
          elem.insert(iter.Item);
           esupp *= iter.Prob;
          Weight += iter.W;
        }
        Weight = Weight / k;


        /*
        some code, and until here I didn't get any problem
        */

            **// This the area that has the problem** 

    S1[elem] = std::move(S1[elem] + esupp);
    Wesupp = Weight * S1[elem];
    S2[elem].first = std::move(S2[elem].first + esupp);
    S2[elem].second = std::make_pair(elem, Wesupp);
  } while (next_combination(data[i].begin(), data[i].begin() + k, data[i].end()));
2
What is the actual line that has the error?Fantastic Mr Fox
Beyond the rest, this line doesn't look right: S2[elem].second = std::make_pair(elem, Wesupp);Marco A.
I would seriously reconsider the data structure design... unordered_set as keys? std::pair<float, std::pair<float, float>>? (at least use a std::tuple or std::array<float, 3>)T.C.
@ben every line from ` S1[elem] = std::move(S1[elem] + esupp)` to ` S2[elem].second = std::make_pair(elem, Wesupp);` cause the errorSandy
@marco-a in the container S2 I would like to insert the unordered_set elem with its three float values. every round in the loop elem has unordered_set, if its similar to the previous one, I will update their three float values, otherwise I will insert new unordered_set with its three float values.Sandy

2 Answers

1
votes

A std::map expects its key to implement the operator <, unless a comparator is supplied.

Your key type, std::unordered_set doesn't implement "less than".

As @T.C. mentions, you could use std::set instead of std::unordered_set.

1
votes

Your current error, as Drew Dormann pointed out, is caused by std::unordered_set's lack of an operator <.

std::set, however, does have an overloaded operator <, so you can use that.

The problem with your code runs deeper than that, though. For instance:

S1[elem] = std::move(S1[elem] + esupp);

You are assigning a float. There's absolutely no point in using std::move, especially as S1[elem] + esupp is already an rvalue. The usual way of writing this line would be S1[elem] += esupp;

S2[elem].first = std::move(S2[elem].first + esupp);

Same problem.

S2[elem].second = std::make_pair(elem, Wesupp);

The LHS of the assignment is a std::pair<float, float>; the RHS creates a std::pair<std::unordered_set<int>, float>.

And as I mentioned in the comments, std::pair<float, std::pair<float, float>> is just bad design. It should be at least std::tuple or even better a std::array<float, 3> or even better a simple struct that makes clear what each of the three floats actually mean.

Further, using a set<int> (unordered or not) as a key in a map is a fairly weird design. Do you want to just maintain a list of set<int>-3xfloat pairings that you can iterate through, or do you actually want to be able to efficiently index with a set<int>? If you don't need the efficient indexing, just use a vector of pairs rather than a map.