c++ - Qt 并在 QList 中查找部分匹配项

标签 c++ qt

我有一个结构即:

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/

相关文章:

c++ - 模板类继承给出未知变量错误

c++ - Beast websocket 惯用关闭?

c++ - 在 Visual Studio C++ 中编译项目时生成目标文件 ".o"

c++ - 我怎样才能延迟Qt中的进程

c++ - 函数的返回类型是重整名称的一部分吗?

c++ - boost 正则表达式匹配

c++ - 如果在 Qt 中设计游戏,QML 是可行的方法吗?

c++ - 如何叠加/覆盖包含透明PNG图像的QLabels?

c++ - 没有这样的插槽 QProgressBar::setValue(const int)

c++ - 设计着色器类