0

Problem description

I defined a std::map as:

struct Key         // Key of my dicitonary
{
    float index[3];
};

struct Value       // Value of my dictionary
{
    float color[3];
    float operator[](int &a) const { return color[a]; }
};

Key key = {a, b, c};
Value value = {d, e, f};

std::map<Key, Value> dict = new std::map<Key,Value>;

if (dict.count(key) < 1) {      // If key does not exist, add value
        addrDict[key] = value;
}

Problem

When I run the code, I get the following error:

d:\documents\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\stl_function.h:386:20: error: no match for 'operator<' (operand types are 'const Key' and 'const Key')

As far as I understand, it cannot compare two Key object in the map, which is ordered. For this reason, I need to implement the operator< in the Key struct (even if I don't care about the order of the keys, but using an unordered map returns compiler errors probably related to g++). I just need to keep the keys composed of three different arrays separated. Any idea on how to achieve that?

What I have done

I implemented the operator with a trial and error approach and this solution kind of return what I am expecting, but it does not store all the possible keys:

bool operator<(const Key &rhs) const {
    (index[0]*index[1]*index[2]) < (rhs.index[0]* rhs.index[1]* rhs.index[2]);
}; 

If, on the other hand, I do this:

bool operator<(const Key &rhs) const {
    index[0] < rhs.index[0];
}; 

I only store a few keys, and not all the possible combinations of keys.

EDIT: solved a problem of variable definition

maurock
  • 527
  • 1
  • 7
  • 22
  • `std::map dict = new std::map;` should just be `std::map dict;`. This looks like it was influenced by Java or a similar language. Edit : `addrDict` is not declared, Did you mean `dict`? – François Andrieux Apr 18 '19 at 15:21
  • `(index[0]*index[1]*index[2]) < (rhs.index[0]* rhs.index[1]* rhs.index[2]);` so `{0.0f, x, y}` and `{0.0f, a, b}` compare equal for any `x`, `y`, `a` and `b`? – YSC Apr 18 '19 at 15:21
  • `std::map dict = new std::map;` -- C++ is not . Also, you could have posted a real [mcve] that shows the issue instead of a jigsaw puzzle we have to piece together and a description. – PaulMcKenzie Apr 18 '19 at 15:22
  • Try simply `using Key = std::array;`. You'll get a built-in comparison operator that will do the right thing. – rustyx Apr 18 '19 at 15:26
  • @rustyx I defined Key as you suggested, but it still returns the same error about the operator – maurock Apr 18 '19 at 15:39
  • @PaulMcKenzie Actually, the original code is much more complex, I use a pointer to the dictionary, and pass the reference to a function that needs to handle the whole mapping. While translating it into a simpler version to describe it here, I did something wrong as you said in the definition. I mainly code in Python, and I am completely new to C++ and still in the learning process. – maurock Apr 18 '19 at 15:43
  • @maurocomi -- Any C++ compiler error can be duplicated by just a few lines of code, maybe a dummy / empty function or two, etc. – PaulMcKenzie Apr 18 '19 at 15:44
  • "*using an unordered map returns compiler errors probably related to g++*" First rule of programming says: "*it is always your fault*". Just as `std::map` requires keys to be comparable with `operator <`, `std::unordered_map` requires keys to have hash function (either as an overload to `std::hash`, or to provide one as template argument for the `std::unordered_map`) – Yksisarvinen Apr 18 '19 at 15:44
  • See [this example](https://gcc.godbolt.org/z/UDRWk7) – rustyx Apr 18 '19 at 15:45
  • @maurocomi [This is what you should be working with](http://coliru.stacked-crooked.com/a/a9aa51d2faf9ce28). That is the crux of the issue -- how to get the key to follow a strict-weak-order so that it satisfies the requirements for `std::map`. – PaulMcKenzie Apr 18 '19 at 15:57
  • @rustyx Thank you, after adjusting the code I have overcome the problem of the `operator<` as you said, and it works. If you write it in the answer sections I can give you the best answer. – maurock Apr 18 '19 at 16:04

0 Answers0