jeudi 5 décembre 2019

How to compare all items in std::map?

I'd like to compare all values in my std::map with each other.

I'm stuck with: for linear containers, like vector, I'd loop over indices i=1; v[i].isUniform(v[i-1]). But I can't do this with maps. I'm looking forward to hear clever ideas.

Here is some pseudocode of what I want to accomplish:

class MyData
{
public:
    bool isUniform(const MyData& other)
    {
        return this->speed == other.speed && this->ban == other.ban;
    }

private:
    bool ban;
    int  speed;
}

std::map<int, MyData> myMap;

bool allUniform = true;
for(item_1, item_2 : myMap) // how to implement this?
{
    if(!item_1.isUniform(item_2))
    {
        allUniform = false;
    }
}

What is the most elegant (readable and efficient) way to do that?

Aucun commentaire:

Enregistrer un commentaire