Qt和在QList中查找部分匹配

8

我有一个结构体 viz:

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工具可用?
3个回答

6

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


我的代码看起来有点不同。 返回 !(fullName.size() && x.fullName.compare(fullName)) && !(probeName.size() && x.probeName.compare(probeName)) && !(format.size() && x.format.compare(format)) && !(source.size() && x.source.compare(source) ); - ExpatEgghead

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;
      }
      // ...
   }
}

@Adam,对一个已排序的向量进行二分查找。当然,我们可以做到这一点。当添加新记录时,我们必须保持顺序。 - Nick Dandoulakis
@Nick D:http://doc.qt.nokia.com/4.6/containers.html#algorithmic-complexity 如果它是一个QList(在这种情况下),你可以这样做。QLinkedList会引起问题。 - Adam W
@Adam,正如我在评论中所说的那样。QList *在其核心中隐藏了一个向量。感谢您指出这一点,我是Qt框架的新手,仍在探索它 :-) - Nick Dandoulakis
啊,我希望我能改变这个结构体,但遗憾的是我不能。 - ExpatEgghead
现在Boost库有正则表达式吗? - ExpatEgghead
显示剩余6条评论

0

类似于Nick D的回答

#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;
}

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


很烦人的是,我无法更改NameKey。 - ExpatEgghead

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接