dimanche 24 mai 2020

C++ elegant way for prioritized search in STL container

I wrote a small function whose aim is to go through a list of elements in order, do some checks on the hashmap value of it and if they pass, return it.

QString ElementContainer::getPreferedAvailableElement() const
{
    QStringList preferred_priority = { A, B, C, D, E };
    foreach(QString element, preferred_priority){
        Element*  data = m_hashmap.value(element, nullptr);
        if(data && data->isReady()){
            return element;
        }
    }
    return QString("");
}

I know that those functional kind of std functions should not be forced no matter if it makes sense or not. I am just curious how you can transform this and if it maybe is more verbose.

Aucun commentaire:

Enregistrer un commentaire