Rafaelpsmed
Rafaelpsmed

Reputation: 67

How to compare 2 QStringList's and show the results?

I want to know how can I compare 2 QStringList and display the results in the following way:

I have 2 QPlainTextEdit A and B

What I want to do is this: take each line in B and compare with all the lines in A. If, in one line-line comparison, all 6 are equal to int n6 (for example) will add 1. If only 5 are equal, the int n5 will add 1 and so on.

I've tried a few things but none of them worked. If you could give some light on this I'll appreciate.

Upvotes: 0

Views: 8972

Answers (2)

Simon Warta
Simon Warta

Reputation: 11408

Start by calculating the number of matching digits per string

int matchingDigits(QString str1, QString str2)
{
    int matches = 0;
    int minSize = str1.size() < str2.size() ? str1.size() : srt2.size();
    for (int pos = 0; pos < minSize; ++pos)
    {
        if (str1.at(pos) == str2.at(pos) ++matches;
    }
    return matches;
}

Now you compare all a strings with each other (iterating over both StringLists) and if matchingDigits() is something > 0 then you can increase your result counters.

QStringList listA;
QStringList listB;

foreach (QString a, listA)
{
    foreach (QString b, listB)
    {
        int matches = matchingDigits(a, b);
        if (matches > 0)
        {
            // do something fancy
        } 
    }
}

Upvotes: 1

Jablonski
Jablonski

Reputation: 18504

Use QSet and subtract() and count() or size()

QStringList mOldList, mNewList;

  mOldList.append("1");
  mOldList.append("2");
  mOldList.append("3");
  mOldList.append("4");
  mOldList.append("5");
  mOldList.append("10");

  mNewList.append("1");
  mNewList.append("2");
  mNewList.append("3");
  mNewList.append("4");
  mNewList.append("5");
  mNewList.append("15");

QSet<QString> subtraction = mNewList.toSet().subtract(mOldList.toSet());
QSet<QString> subtraction1 = mOldList.toSet().subtract(mNewList.toSet());
foreach (const QString &filename, subtraction)
        qDebug() << " difference: "<<  filename;
foreach (const QString &filename, subtraction1)
        qDebug() << " difference: "<<  filename;

Result:

 difference:  "15" 
 difference:  "10" 

For example with next lists:

  mOldList.append("1");
  mOldList.append("2");
  mOldList.append("3");
  mOldList.append("4");
  mOldList.append("5");
  mOldList.append("10");

  mNewList.append("1");
  mNewList.append("2");
  mNewList.append("3");
  mNewList.append("4");
  mNewList.append("5");
  mNewList.append("15");

QSet<QString> subtraction = mNewList.toSet().subtract(mOldList.toSet());
foreach (const QString &fileName, subtraction)
        qDebug() << " difference: "<<  fileName;

Result only 15:

 difference:  "15" 

It means that one element is not same.

  mOldList.append("1");
  mOldList.append("2");
  mOldList.append("3");
  mOldList.append("4");
  mOldList.append("5");
  mOldList.append("10");

  mNewList.append("1");
  mNewList.append("2");
  mNewList.append("3");
  mNewList.append("4");
  mNewList.append("115");
  mNewList.append("15");

Two elements are not same:

 difference:  "115" 
 difference:  "15"

Or another way:

qSort(mNewList);
qSort(mOldList);

if(mNewList == mOldList){
    qDebug() << "same";
}
else{
    qDebug() << "not same";
}

Upvotes: 2

Related Questions