2010-06-07 65 views
6

我有一个结构即发现部分匹配:Qt和在的QList

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

这是一个的QList举行:

QList<NameKey> keyList; 

什么,我需要做的是在密钥列表找到occurence部分匹配,其中的搜索是仅填充了两个成员的NameKey。 所有keyList条目都是完整的NameKey。

我现在的实施很糟糕,太多了,如果条件和条件太多。所以,如果我有一个全名和格式的DataKey我需要找到keyList中匹配的所有出现。 任何有用的Qt/boost事物可用?

回答

3

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兼容性。

+0

这看起来很酷!谢谢。 – ExpatEgghead 2010-06-08 02:16:02

+0

我看起来有点不同。 (fullName))&& !(probeName.size()&& x.probeName.compare(probeName))&& !(format.size()&& x。 format.compare(format))&& !(source.size()&& x.source.compare(source)); – ExpatEgghead 2010-06-08 04:14:52

4

请注意:任何使用列表的解决方案至少具有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; 
     } 
     // ... 
    } 
} 
+0

@Nick D:您可能想澄清一下,使用列表的这个问题的任何解决方案都至少具有O(n)时间复杂度。在其他情况下,这不是事实。 – 2010-06-07 17:29:29

+0

@Adam,如果我们将NameKey记录存储在列表容器中,除了在搜索匹配项时遍历该列表,我们没有其他选择。除非我们使用辅助容器(即Map或Trie)来存储额外的信息以加速搜索,否则我没有看到更快的实现方式。我错过了什么? – 2010-06-07 17:40:03

+0

@Nick D:一个例子是如果您的列表排序,您可以使用二进制搜索并获得O(log n)。或者就像你说的那样,如果有索引结构来加速搜索。 – 2010-06-07 17:51:33

0

类似Nick D's answer

#include <QList> 
#include <QString> 
#include <QRegExp> 

struct NameKey 
{ 
    QString fullName; 
    QString probeName; 
    QString format; 
    QString source; 

    bool containsPattern(const QRegExp &pattern) { 
     return fullName.contains(reg) || 
       probeName.contains(reg) || 
       format.contains(reg) || 
       source.contains(reg); 
    } 
}; 

QList<NameKey> matches(const QList<NameKey> &keyList, const QString &pattern) { 
    QRegExp reg(pattern); 
    QList<NameKey> matches; 
    foreach (NameKey nk, keyList) { 
     if (nk.containsPattern(reg)) 
     matches << nk; 
    } 
    return matches; 
} 

显然有很多方法可以做到这一点。我喜欢尽可能多地将数据结构放入数据结构中。

+0

令人讨厌的是我无法更改NameKey。 – ExpatEgghead 2010-06-08 02:13:56