PDA

View Full Version : append vs. << for QStringList



ce_nort
14th April 2016, 15:38
Newbie understanding question. I'm using QStringList as an example, but it's kind of a general question.
If I have a QStringList that I want to populate, I can do:


QStringList myList;
myList << "String1" << "String2";

Or I can do:


QStringList myList;
myList.append("String1");
myList.append("String2");

Obviously the first one is more concise, but is there any behind the scenes benefit to one approach or the other? When would I use one vs the other and why?

wysota
14th April 2016, 16:16
They are equivalent.

anda_skoa
14th April 2016, 16:26
Those are basically equivalent, the operator<<() approach is just for added convenience in case you want to add multple entries.

It also can be used for things like on-the-fly creation of a QStringList


void someFunction(const QStringList &list);

// pass a list without creating a local variable first
someFunction(QStringList() << "a" << "b");


Cheers,
_