0

I have the following code, I've got some errors like:

struct duplicatedTurns
    {
        int nodeId;
        int min;
        int max;

        bool operator==(const duplicatedTurns& other) const
        {
            return nodeId == other.nodeId && min == other.min && max == other.max;
        }

I solved it here to following code:
bool operator<(const duplicatedTurns& other) const
{
if (nodeId != other.nodeId) return nodeId < other.nodeId;
if (min != other.min) return min < other.min;
if (max != other.max) return max < other.max;
return false;
}

    };

The container that I want to use:

std::map<duplicatedTurns, int> selected;

After i would like to insert elements there:

selected.insert(duplicatedturns{it->nodeId, std::min(it->toLinkId, it->fromLinkId), std::max(it->toLinkId, it->fromLinkId)}, "here: increment the number if the key are the same" );
  • 2
    "Some errors" is not very helpful. What errors are you getting? – Putnam Aug 28 '20 at 12:13
  • 3
    To use a `struct` as a key, you need to have a comparison operator `<`. – Jan Schultke Aug 28 '20 at 12:14
  • There must be an overload of `operator<` for the key of `std::map` . Your key only overloads `operator==` (as far as we can see) – Lukas-T Aug 28 '20 at 12:14
  • This is the first time I ever use struct as key. I saw that question and asnwares. I have multiple variables and I don't understand how implement that. – theshepherd Aug 28 '20 at 12:17
  • If you don't know how to compare your struct than how should anyone else? There is not much of differnce to `operator==` – Thrasher Aug 28 '20 at 12:18
  • 1
    I was about to write an answer. I got [this](https://godbolt.org/z/Mq1hG6) far. Perhaps it's enough to get you going. – Ted Lyngmo Aug 28 '20 at 12:35
  • The error doesn't mention anything about `operator<`, the error is about invalid use of `insert`. Change it to `emplace`, or wrap your arguments in `{}` to construct an `std::pair` object. – Yksisarvinen Aug 28 '20 at 12:36
  • 1
    @TedLyngmo. Thank you this is what i mean for. I figured it out by the way. – theshepherd Aug 28 '20 at 12:45
  • 1
    @MarcellJuhasz Great! Happy hacking! :-) You recommend using `std::tie` for the logic in `operator<` though. It makes it _a lot_ easier to read and maintain. It's also very easy to make errors when creating the operator to comply with the _strict weak ordering_ a map requires. – Ted Lyngmo Aug 28 '20 at 12:46
  • "You" should be "I" above :-) – Ted Lyngmo Aug 28 '20 at 12:56

2 Answers2

1

How can I use struct as key in a std::map?

Either by making the class less-than comparable using the default comparison function of std::map which is std::less (this can be achieved by defining overload for operator< which satisfies the specified requirements), or by providing a custom comparison function as a template argument for std::map.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

In C++ Map is ordered by default. That's why operator< has to be defined, so it could sort elements.