我有一个结构即:
struct NameKey
{
std::string fullName;
std::string probeName;
std::string format;
std::string source;
}
保存在 QList 中:
QList<NameKey> keyList;
我需要做的是在部分匹配的 keyList 中找到一个事件,其中搜索的是仅填充了两个成员的 NameKey。 所有 keyList 条目都是完整的 NameKey。
我目前的实现是,好吧,有太多的 if 和条件,非常无聊。
因此,如果我有一个带有全名和格式的 DataKey,我需要在 keyList 中找到匹配的所有匹配项。 有什么有用的 Qt/boost 可用吗?
最佳答案
QList
与 STL 兼容。所以您可以将它与 STL 算法一起使用:
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", ""));
不过,我不知道 Qt 是否会主动维护 STL 兼容性。
关于c++ - Qt 并在 QList 中查找部分匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2990051/