I have four std::vector containers that all might (or might not) contain elements. I want to determine which of them has the most elements and use it subsequently.
I tried to create a std::map with their respective sizes as keys and references to those containers as values. Then I applied std::max on the size() of each vector to figure out the maximum and accessed it through the std::map.
Obviously, this gets me into trouble once there is the same number of elements in at least two vectors.
Can anyone think of a elegant solution ?
You’re severely overthinking this. You’ve only got four vectors. You can determine the largest vector using 3 comparisons. Just do that:
EDIT:
Now with pointers!
EDIT (280Z28):Now with references! 🙂
EDIT:
The version with references won’t work. Pavel Minaev explains it nicely in the comments:
The pointer version is likely your best bet: it’s quick, low-cost, and simple.