Dcow
Dcow

Reputation: 1463

Best container for string search in Qt5

In my project I need to determine occurrence of the string in the list of strings. The duplicates in the list are not allowed, and the order is irrelevant.

Please help me choose the best Qt container for the string search.

Upvotes: 0

Views: 1039

Answers (1)

TheDarkKnight
TheDarkKnight

Reputation: 27629

If you want a list of strings, Qt provides the QStringList class.

Once all strings are added, you can call the removeDuplicates function to satisfy your requirement of no duplicates.

To search for a string, call the filter function, which returns a list of strings containing the string, or regular expression passed to the function.

Here's an example, adapted from the Qt documentation: -

// 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");

result is a QStringList containing "Bill Murray" and "Bill Clinton"

If you just want to know if a string is in the list, use the contains function

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

Found will return true.

Upvotes: 2

Related Questions