C++ Qt和在QList中查找部分匹配
我有一个结构,即:C++ Qt和在QList中查找部分匹配,c++,qt,C++,Qt,我有一个结构,即: struct NameKey { std::string fullName; std::string probeName; std::string format; std::string source; } 保存在QList中的: QList<NameKey> keyList; QList键列表; 我需要做的是在部分匹配的keyList中查找一个事件,其中搜索的是只填充了两个成员的Nam
struct NameKey
{
std::string fullName;
std::string probeName;
std::string format;
std::string source;
}
保存在QList中的:
QList<NameKey> keyList;
QList键列表;
我需要做的是在部分匹配的keyList中查找一个事件,其中搜索的是只填充了两个成员的NameKey。
所有密钥列表条目都是完整的NameKey
我目前的实现非常无聊,有太多的if和条件
因此,如果我有一个带有全名和格式的DataKey,我需要在keyList中找到匹配的所有事件。
有什么有用的Qt/boost工具吗?请注意:任何使用列表的解决方案至少都有O(n)个时间复杂性 一种选择是使用
QString
,而不是std::string
,并利用其内置的正则表达式支持
例如:
#include <QList>
#include <QString>
#include <QRegExp>
struct NameKey
{
QString fullName;
QString probeName;
QString format;
QString source;
};
QList<NameKey> keyList; // <--
void Foo() {
QRegExp reg("pattern"); // <-- prepare a regular expression (format)
NameKey nk;
foreach (nk, keyList) {
if (nk.fullName.contains(reg)) {
// a match ...
break;
}
// ...
}
}
#包括
#包括
#包括
结构名称键
{
QString全名;
QString probeName;
QString格式;
QString源;
};
QList键列表;// 类似于:
#包括
#包括
#包括
结构名称键
{
QString全名;
QString probeName;
QString格式;
QString源;
bool containsPattern(常数和模式){
返回全名。包含(reg)||
probeName.contains(注册表)||
格式.包含(reg)||
来源.包含(reg);
}
};
QList匹配(常量QList和键列表、常量QString和模式){
QRegExp reg(模式);
QList匹配;
foreach(名称键nk、键列表){
如果(自然灾害)
匹配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", ""));
struct NameKeyMatch{
NameKeyMatch(常量std::string&s1、常量std::string&s2、常量std::string&s3、常量std::string&s4)
:全名(s1)、probeName(s2)、格式(s3)、源(s4){}
布尔运算符()(常量NameKey&x)常量
{
返回fullName.size()&&x.fullName==fullName&&
probeName.size&&x.probeName==probeName&&
format.size&&x.format==格式&&
source.size&&x.source==source;
}
std::字符串全名;
std::字符串probeName;
std::字符串格式;
std::字符串源;
};
QList::iterator i=std::find_if(keyList.begin()、keyList.end()、NameKeyMatch(“全名”、“格式”、“格式”));
我不知道Qt是否会积极维护STL兼容性。@Nick D:您可能想澄清,使用列表解决此问题的任何解决方案都会有O(n)至少是时间复杂度。在其他情况下,情况并非如此。@Adam,如果我们将NameKey记录存储在列表容器中,我们除了在搜索匹配项时迭代该列表之外别无选择。除非我们使用辅助容器(如映射或Trie)存储额外信息以加快搜索速度,否则我看不到更快的实现方法。是吗我错过了什么?@Nick D:一个例子是,如果你的列表被排序,你可以使用二进制搜索得到O(logn)。或者像你说的,如果有一个索引结构来加快搜索速度。@Adam,二进制搜索一个排序向量。当然我们可以这样做。当然,我们在添加新记录时必须保持顺序。@Nick D:我理解,我只是指出,“任何使用列表的解决方案至少都有O(n)个时间复杂度。”假设条件(例如未排序的列表)。令人恼火的是,我无法更改NameKey.mine看起来有点不同。return!(fullName.size()&&x.fullName.compare(fullName))&&!(probeName.size()&&x.probeName.compare(probeName))&&!(format.size()&&x.format.compare(format))&!(source.size()和&x.source.compare(source));
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", ""));