Skip to content

Instantly share code, notes, and snippets.

@mitchcurtis
Created June 22, 2025 03:55
Show Gist options
  • Save mitchcurtis/d84294c89839befdb96d2956e045e456 to your computer and use it in GitHub Desktop.
Save mitchcurtis/d84294c89839befdb96d2956e045e456 to your computer and use it in GitHub Desktop.
Getting additions and removals from two lists
#include <QCoreApplication>
#include <QDebug>
void printChanges(QList<QString> existingList, QList<QString> newList)
{
std::sort(existingList.begin(), existingList.end());
std::sort(newList.begin(), newList.end());
QList<QString> added;
std::set_difference(newList.constBegin(), newList.constEnd(),
existingList.constBegin(), existingList.constEnd(), std::back_inserter(added));
qDebug() << "added" << added;
QList<QString> removed;
std::set_difference(existingList.constBegin(), existingList.constEnd(),
newList.constBegin(), newList.constEnd(), std::back_inserter(removed));
qDebug() << "removed" << removed << "\n";
}
int main(
int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// Expected output:
// added QList()
// removed QList()
printChanges({ "a", "b" }, { "a", "b" });
// Expected output:
// added QList("c")
// removed QList()
printChanges({ "a", "b" }, { "a", "b", "c" });
// Expected output:
// added QList()
// removed QList("b")
printChanges({ "a", "b" }, { "a" });
// Expected output:
// added QList()
// removed QList()
printChanges({ "a", "b" }, { "b", "a" });
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment