0

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

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

4

1 回答 1

2

如果你想要一个字符串列表,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

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

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

找到会返回真。

于 2013-09-23T08:56:36.250 回答