Qt5 中字符串搜索的最佳容器

Best container for string search in Qt5

本文关键字:最佳 搜索 字符串 Qt5      更新时间:2023-10-16

在我的项目中,我需要确定字符串列表中字符串的出现次数。不允许列表中的重复项,并且顺序无关紧要。

请帮我为字符串搜索选择最好的Qt容器。

如果你想要一个字符串列表,Qt提供了QStringList类。

添加所有字符串后,可以调用 removeDuplicates 函数来满足无重复项的要求。

若要搜索字符串,请调用 filter 函数,该函数返回包含字符串的字符串列表或传递给函数的正则表达式。

下面是一个改编自Qt文档的示例:

// create the list and add strings
QStringList list;
list << "Bill Murray" << "John Doe" << "Bill Clinton";
// Oops...added the same name
list << "John Doe";
// remove any duplicates
list.removeDuplicates();
// search for any strings containing "Bill"
QStringList result;
result = list.filter("Bill");

结果是一个包含"Bill Murray"和"Bill Clinton"的QStringList。

如果您只想知道字符串是否在列表中,请使用 包含函数

bool bFound = list.contains("Bill Murray");

找到将返回 true。