Results 1 to 3 of 3

Thread: append vs. << for QStringList

  1. #1
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    52
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Question append vs. << for QStringList

    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:
    Qt Code:
    1. QStringList myList;
    2. myList << "String1" << "String2";
    To copy to clipboard, switch view to plain text mode 
    Or I can do:
    Qt Code:
    1. QStringList myList;
    2. myList.append("String1");
    3. myList.append("String2");
    To copy to clipboard, switch view to plain text mode 
    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: append vs. << for QStringList

    They are equivalent.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    ce_nort (14th April 2016)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: append vs. << for QStringList

    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
    Qt Code:
    1. void someFunction(const QStringList &list);
    2.  
    3. // pass a list without creating a local variable first
    4. someFunction(QStringList() << "a" << "b");
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    ce_nort (14th April 2016)

Similar Threads

  1. How to append an xml file
    By athulms in forum Newbie
    Replies: 2
    Last Post: 29th August 2011, 08:05
  2. Can't see any text append in UI?
    By SamT in forum Qt Programming
    Replies: 3
    Last Post: 9th April 2011, 12:27
  3. Append to a XML Document
    By Goldmmbr in forum Qt Programming
    Replies: 0
    Last Post: 16th November 2009, 15:03
  4. Cannot append to QFile using QIODevice::Append
    By philwinder in forum Qt Programming
    Replies: 4
    Last Post: 17th November 2008, 09:09
  5. Replies: 7
    Last Post: 2nd June 2006, 12:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.