Results 1 to 6 of 6

Thread: What's the difference between emplace & insert in std::vector or in std::list

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: What's the difference between emplace & insert in std::vector or in std::list

    In both cases, the vector or list will be grown if necessary to accommodate the new entry. size() will increase by 1, but capacity() may not if the underlying implementation's default allocation size is already big enough to hold the extra element or if you called reserve() with a larger value than the size() before the new element was added.

    v.emplace(v.end(), m); // "emplace() constructs a new instance in place" --- I have passed a already constructed element.
    Ah, but here you are implicitly invoking the MyClass copy constructor because you are passing a MyClass instance as an argument:

    Qt Code:
    1. MyClass::MyClass( const MyClass & myClass )
    To copy to clipboard, switch view to plain text mode 

    so you end up with two instances, "m" - the one you constructed manually, and a copy of "m", built in place in the vector and initialized with the contents of "m". Even though your class doesn't explicitly define a copy constructor, C++ implements one for you (which by default makes a bitwise copy of the contents of the instance it is initialized with). Likewise for operator=().

    To explain in another way:

    insert() requires an already-existing instance of the class, which it then makes a copy of into the vector (using MyClass:: operator=( const MyClass & ))
    emplace() does not require an existing instance, but creates one for you and initializes it with the extra arguments you pass in (using MyClass:: MyClass( arguments ))
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. #2
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: What's the difference between emplace & insert in std::vector or in std::list

    Ok, now I got it.
    After reading your answer, I now tried with explicit constructor and it reported error for passing int value at insert. Now it makes sense to me.
    Qt Code:
    1. explicit MyClass(int i = 0) : d_val(i) {}
    2. ...
    3. ...
    4. v.insert(v.end(), 23); // compiler reports error
    To copy to clipboard, switch view to plain text mode 
    Thanks a lot, d_stranz

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: What's the difference between emplace & insert in std::vector or in std::list

    Yes, I missed your first example of insert( v.end(), 23 ). That would invoke the default constructor to create a temporary instance initialized to 23, which would then be copied into the vector.

    Glad to help.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Copying vector of pointers to another vector
    By Momergil in forum Newbie
    Replies: 12
    Last Post: 24th September 2011, 22:09
  2. Insert custom widget in the qt designer list box
    By eg3gg in forum Qt Programming
    Replies: 12
    Last Post: 6th September 2010, 09:06
  3. Replies: 2
    Last Post: 20th August 2010, 13:20
  4. Vector vs List
    By phillip_Qt in forum Qt Programming
    Replies: 6
    Last Post: 9th October 2009, 05:10
  5. insert in a vector of vector
    By mickey in forum General Programming
    Replies: 3
    Last Post: 6th March 2007, 08:45

Tags for this Thread

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.