Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt and finding partial matches in a QList

Tags:

c++

qt

I have a struct viz:

struct NameKey
{
    std::string      fullName;
    std::string      probeName;
    std::string      format;
    std::string      source;
}

which are held in a QList:

QList<NameKey> keyList;

what I need to do is find an occurence in keyList of a partial match where the search is for a NameKey that only has two members filled. All the keyList entries are full NameKey's.

My current implementation is , well, boring in the extreme with too many if's and conditions.

So, If I have a DataKey with a fullName and a format I need to find all the occurences in keyList which match. Any useful Qt/boost things available?

like image 213
ExpatEgghead Avatar asked Jun 07 '10 13:06

ExpatEgghead


Video Answer


1 Answers

QList is compatible with STL. So you can use it with STL algorithm:

struct NameKeyMatch {
    NameKeyMatch(const std::string & s1, const std::string & s2, const std::string & s3, const std::string & s4)
    : fullName(s1), probeName(s2), format(s3), source(s4) {}

    bool operator()(const NameKey & x) const
    {
        return  fullName.size() && x.fullName == fullName &&
                probeName.size && x.probeName == probeName &&
                format.size && x.format == format &&
                source.size && x.source == source;
    }

    std::string fullName;
    std::string probeName;
    std::string format;
    std::string source;
};

QList<int>::iterator i = std::find_if(keyList.begin(), keyList.end(), NameKeyMatch("Full Name", "", "Format", ""));

I don't know if Qt will actively maintain STL compatibility though.

like image 136
Stephen Chu Avatar answered Sep 17 '22 16:09

Stephen Chu