mercredi 21 octobre 2020

What is the fastest way to check if all elements in vector have the same value in c++ [duplicate]

Note: vector<int> vec = {0, 0, 0} return true

So I have a solution, it looks like this:

bool check(vector<char> vec)// can't think of a better name
{
    char firstChar {vec[0]};
    for (char i : vec)
    {
        if (i != firstChar)
            return false;
    }
    return true;
}

But since I'm a newbie, is there a better solution?

Aucun commentaire:

Enregistrer un commentaire