c++ - 在 QStringList 中搜索特定项目,然后搜索可能包含项目的其他项目

标签 c++ qt

我想获取搜索的所有“INDICES”。显然“QStringList::indexOf”一次返回一个索引……所以我必须做一个 while 循环。但它也“只”进行精确匹配。

如果我想返回所有包含“husky”的项的索引怎么办...然后可能是“dog”...然后是“dog 2”。 我是否坚持使用“QString::contains”然后循环来完成此操作?还是我缺少更多与“QStringList 类”相关的方式

QStringList dogPound;
dogPound    << "husky dog 1"
            << "husky dog 2"
            << "husky dog 2 spotted"
            << "lab dog 2 spotted";

最佳答案

您可以使用QStringList::filter 方法。它返回一个新的 QStringList,其中包含从过滤器传递的所有项目。

QStringList dogPound;
dogPound    << "husky dog 1"
            << "husky dog 2"
            << "husky dog 2 spotted"
            << "lab dog 2 spotted";

QStringList spotted = dogPound.filter("spotted");
// spotted now contains "husky dog 2 spotted" and "lab dog 2 spotted"

关于c++ - 在 QStringList 中搜索特定项目,然后搜索可能包含项目的其他项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18082888/

相关文章:

c++ - 更新 sourceModel() 而不在 QSortFilterProxyModel 上应用过滤

linux - GTK 和 Qt 的功能区界面

qt - 专注于 PyQt 中的 tabified QDockWidget

javascript - 如何使用 C++ 通过 websocket 发送数据?

c++ - SPOJ上通过TLE的代码优化建议

c++ - 在 Windows 中加速 printf 和 cout

c++ - 在 Qt 应用程序中使用 glad loader

c++ - 使用回调函数从构造函数调用虚拟/派生方法的替代方法?

c++ - 模运算——竞技编程

c++ - 单例线程安全程序