使用QSet 和subtract() 和count() 或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;
结果:
difference: "15"
difference: "10"
例如下一个列表:
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;
结果只有 15:
difference: "15"
表示一个元素不相同。
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");
两个元素不相同:
difference: "115"
difference: "15"
或者其他方式:
qSort(mNewList);
qSort(mOldList);
if(mNewList == mOldList){
qDebug() << "same";
}
else{
qDebug() << "not same";
}