我有一个结构即:
struct NameKey
{
std::string fullName;
std::string probeName;
std::string format;
std::string source;
}
Run Code Online (Sandbox Code Playgroud)
在QList中保存:
QList<NameKey> keyList;
Run Code Online (Sandbox Code Playgroud)
我需要做的是在部分匹配的keyList中找到一个出现,其中搜索是仅填充了两个成员的NameKey.所有keyList条目都是完整的NameKey.
我目前的实施情况很糟糕,如果有太多的条件和条件,那就太无聊了.
所以,如果我有一个带有fullName和格式的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", ""));
Run Code Online (Sandbox Code Playgroud)
我不知道Qt是否会主动维护STL兼容性.