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