Results 1 to 6 of 6

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

  1. #1
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    247
    Thanks
    90
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

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

    As stated in the question, can someone elaborate & help me understand the difference between emplace & insert member functions in vector or list.
    How are they different from each other?
    Thanks.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,942
    Thanks
    231
    Thanked 619 Times in 609 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

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

    The difference is that insert() takes a reference to an already-constructed instance and copies it into the vector or list before the given position. emplace() constructs a new instance in place before the given position in the vector or list. You do not give it an instance to copy in, you give it arguments which will be passed to the constructor of the new instance. So in effect, emplace() grows the vector or list by enough to hold a new instance and then constructs the instance in the new empty spot.

    Qt Code:
    1. MyClass myClass( 42 ); // MyClass::MyClass( int value = 0 )
    2. std::vector< MyClass > myClasses;
    3.  
    4. myClasses.insert( myClasses.end(), myClass ); // makes a copy of myClass at the end of the vector
    5. myClasses.emplace( myClasses.end(), 43 ); // constructs a new MyClass instance at the end of the vector and initializes it with value 43
    6. myClasses.emplace( myClasses.end() ); // uses the default MyClass constructor to create a new instance at the end of the vector
    To copy to clipboard, switch view to plain text mode 

    In both cases, insert() and emplace() return an iterator pointing to the new element.
    Last edited by d_stranz; 28th October 2017 at 01:02.
    <=== 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.

  3. #3
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    247
    Thanks
    90
    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

    After reading the definition, I'm still unclear about the concept. This is what I grasped, correct me if I'm wrong.
    "takes a reference to an already-constructed instance and copies it into ... " - does this mean, it consumes one element space in currently allocated storage ?
    "emplace constructs a new instance" - does that mean, it keeps the "capacity" undisturbed, adds one element exclusively, irrespective of currently allocated storage ?

    Here is what I tried -
    Qt Code:
    1. class MyClass
    2. {
    3. int d_val;
    4. public:
    5. MyClass(int i = 0) : d_val(i) {}
    6. int getVal() { return d_val; }
    7. };
    8.  
    9. int main()
    10. {
    11. MyClass m(99);
    12. vector<MyClass> v;
    13. v.insert(v.end(), 23); // "insert() takes a reference to an already-constructed instance" --- But I have passed a unconstructed element.
    14. v.emplace(v.end(), m); // "emplace() constructs a new instance in place" --- I have passed a already constructed element.
    15.  
    16. for(auto i : v)
    17. cout << i.getVal() << endl;
    18. }
    To copy to clipboard, switch view to plain text mode 

    I'm still unable to comprehend the difference.
    Thanks.
    Last edited by rawfool; 28th October 2017 at 06:24.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,942
    Thanks
    231
    Thanked 619 Times in 609 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    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.

  5. #5
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    247
    Thanks
    90
    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

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,942
    Thanks
    231
    Thanked 619 Times in 609 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    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.