Best container for string search in Qt5 - c++

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.

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.

Related

Pack QStringList to QString and unpack it back

I'm in search for an easy and foolproof way to convert an arbitrary QStringList to a single QString and back.
QStringList fruits;
fruits << "Banana", "Apple", "Orange";
QString packedFruits = pack(fruits);
QStringList unpackFruits = unpack(packedFruits);
// Should be true
// fruits == unpackFruits;
What might be the easiest solution for this kind of problem?
From QStringList to QString - QStringList::join:
Joins all the string list's strings into a single string with each element separated by the given separator (which can be an empty string).
QString pack(QStringList const& list)
{
return list.join(reserved_separator);
}
From QString to QStringList - QString::split:
Splits the string into substrings wherever sep occurs, and returns the list of those strings. If sep does not match anywhere in the string, split() returns a single-element list containing this string.
QStringList unpack(QString const& string)
{
return string.split(reserved_separator);
}
Previous answers mentioned QString::split and QStringList::join which is the correct way, but if the separator you choose is included in any of the strings it will break your conversion.
You must prevent strings in the list to contain your separator with one of the following techniques:
Throw an error before QStringList::join if any string includes the separator
Ensure they can not contain the separator (for example storing the string with its QByteArray::toHex(myString.toLatin1()) representation, then using a separator that has character(s) outside of the range 0..9 and a..f. Then convert back with QString::fromLatin1(QByteArray::fromHex(myHexString)) afterward
Use any separator regardless if the strings contain it, but implement an escape logic for it before the join(), and an un-escape logic after the split(), so that the separator is never present in any of the strings at the time of join, but all instances of it will be restored.
Use QStringList::join() :
QStringList strList;
strList << "Banana" << "Apple" << "Orange" ;
QString str = strList.join(""); // str = "BananaAppleOrange";
str = strList.join(","); // str = "Banana,Apple,Orange";

using multiple characters as delimiters in Coldfusion list

I am trying to use multiple characters as the delimeter in ColdFusion list like ,( comma and blank) but it ignores the blank.
I then tried to use:
<cfset title = listappend( title, a[ idx ].title, "#Chr(44)##Chr(32)#" ) />
But it also ignores the blank and without blanks the list items to diffucult to read.
Any ideas?
With ListAppend you can only use one delimiter. As the docs say for the delimiters parameter:
If this parameter contains more than one character, ColdFusion uses only the first character.
I'm not sure what a[ idx ].title contains or exactly what the expected result is (would be better if you gave a complete example), but I think something like this will do what you want or at least get you started:
<cfscript>
a = [
{"title"="One"},
{"title"="Two"},
{"title"="Three"}
];
result = "";
for (el in a) {
result &= el.title & ", ";
}
writeDump(result);
</cfscript>
I think there's a fundamental flaw in your approach here. The list delimiter is part of the structure of the data, whereas you are also trying to use it for "decoration" when you come to output the data from the list. Whilst often conveniently this'll work, it's kinda conflating two ideas.
What you should do is eschew the use of lists as a data structure completely, as they're a bit crap. Use an array for storing the data, and then deal with rendering it as a separate issue: write a render function which puts whatever separator you want in your display between each element.
function displayArrayAsList(array, separator){
var list = "";
for (var element in array){
list &= (len(list) ? separator : "");
list &= element;
}
return list;
}
writeOutput(displayAsList(["tahi", "rua", "toru", "wha"], ", "));
tahi, rua, toru, wha
Use a two step process. Step 1 - create your comma delimited list. Step 2
yourList = replace(yourList, ",", ", ", "all");

Compare string "A" to string "B" in coldfusion

Is there a function in Coldfusion that will take 2 strings and figure out which is the 'higher' alphabetically . So if I had "Daniel" and "John", it would return Daniel?
Put your strings into an array then use arraySort(). (example not tested)
var names = ['Daniel', 'John'];
arraySort( names, 'textnocase' );
writeOutput(names[1]);

How to find elements of a list are subset of string using python

I am a Molecular Biologist and new to programming, so excuse me for my language. I am working with python.
Example:
string = "gctatagcgttatatactagcctatagctata"
list = ["gtagctaggac", "mptalltruiworw", "12365478995", "nvncmvncmvncmvn"]
now coming to question
I want to know a method which can discover that
for element in list:
if element is subset of string (in any order)
return element
In above example the answer should be
gtagctaggac
Rather than spend time generating permutations, compare the number of letters in the list element and the string. Note that this code doesn't check for letters in the string that are not in the pattern.
string = "gctatagcgttatatactagcctatagctata"
list = ["gtagctaggac", "mptalltruiworw", "12365478995", "nvncmvncmvncmvn"]
from collections import defaultdict
def count_letters(string):
counts = defaultdict(int)
for letter in string:
counts[letter] += 1
return counts
sc = count_letters(string)
for element in list:
counts = count_letters(element)
if all([sc[letter] >= counts[letter] for letter in counts]):
print "Found", element
As a matter of style, it's better not to use the names of built-in classes like "list" and "string".

ViM: how to put string from input dialog in a list

VIM: Does anyone know how to put a string from an input dialog in a list?
p.e.:
the string "3,5,12,15"
to:
list item[1] = 3
list item[2] = 5
list item[3] = 12
etc.
and how can I know how many list items there are?
From :h E714
:let l = len(list) " number of items in list
:let list = split("a b c") " create list from items in a string
In your case,
let string = "3,5,7,19"
let list = split(string, ",")
echo len(list)
Use split, len and empty functions:
let list=split(string, ',')
let list_length=len(list)
" If all you want is to check whether list is empty:
if empty(list)
throw "You must provide at least one value"
endif
Note that if you want to get a list of numbers out of the string, you will have to use map to transform list elements into numbers:
let list=map(split(string, ','), '+v:val')
Most of time you can expect strings be transformed into numbers, but sometimes such transformation is not done.