First of all i'm a complete beginner in C++ and Qt and i'm using Qt 6.2 and C++11. This is the code that i have problem with:
QSet<QList<QString>> listSet;
for(int i = 0; i < 10; i++)
{
QList<QString> myList;
for(int r = 0; r < 10; r++)
{
myList << "Item" + QString::number(r);
}
listSet.insert(myList);
}
qInfo() << listSet.count();
I was expecting that i would get the output of "10" but instead i got "1". I changed the code to this and it fixed the problem but i just can't wrap my head around it:
QSet<QList<QString>> listSet;
for(int i = 0; i < 10; i++)
{
QList<QString> myList;
myList << "default" + QString::number(i);
for(int r = 0; r < 10; r++)
{
myList << "Item" + QString::number(r);
}
listSet.insert(myList);
}
qInfo() << listSet.count();
i want to know why C++ is behaving like this.
QSetis a collection of unique objects. The first code snipped produces 10 equal to each othermyListobjects. Thus,QSetgets only one uniquemyListobject:qInfo() << listSet.count();outputs 1.The second snippet makes not equal
myListobjects, they differ by the first list items, andqInfo() << listSet.count();outputs 10.