PDA

View Full Version : QList/QStringList comparison question



di_zou
27th October 2009, 17:58
Is there a built in Qt function that compares two QLists or two QStringLists?
I have two QStringLists, list1 and list2. I want to check if every QString in list1 is in list2 and if every QString in list2 is in list1. Order does not matter. Is there a built in Qt function or a built in C++ function to do this. Right now I am using for loops to do this, and I would rather not do this.

calhal
27th October 2009, 22:51
Is there a built in Qt function that compares two QLists or two QStringLists?
QtAssistant says:


bool QList::operator== ( const QList<T> & other ) const

Returns true if other is equal to this list; otherwise returns false.

Two lists are considered equal if they contain the same values in the same order.

This function requires the value type to have an implementation of operator==().

wysota
28th October 2009, 00:16
To complement what Artur already said, if you don't care about the order, you need to sort the two lists prior to comparing them.


QStringList list1, list2;
qSort(list1);
qSort(list2);
if(list1==list2){
// ...
}

Bakuriu
28th October 2009, 14:03
I think you can do something like this:



int check_lists(QStringList & list1, QStringList & list2) {
int len_1 = list1.count();
int len_2 = list2.count();
int index;
if (len_1 <= len_2) {
for (index = 0;x < len_1; ++x) {
if (!list2.contains(list1[index]))
return 0;
else {
for (index = 0; x < len_2; ++x) {
if (!list1.contains(list2[index]));
return 0;
return 1;
}

wysota
28th October 2009, 14:51
No, you can't.

Try to compare the following lists your way:
1. a, a, b
2. a, b, b

The lists are different, yet your comparison will say they are the same.
Even if you consider the two above lists to be equal, the following two are certainly not and your function will still say they are:
1. a, b, c
2. a, b, c, d

Bakuriu
28th October 2009, 15:35
Ops...
I thought it was:
"I want to see if all elements of list1 are in list2 with "order-insensitive" mode."
and not:
"I want to see if all elements of list1 are in list2 and viceversa with "order-insesitive" mode."
sry ^^