LearnFrenzy
Sign in Take a free test

STL Containers · STL Containers MCQ · Question 13

emplace_back versus push_back Answer, explanation & discussion

  • MCQ
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 13 Back to Exercise

What is the difference between v.emplace_back(1, "pen") and v.push_back(Order(1, "pen"))?

  • A No difference
  • B emplace_back constructs the element in place from the arguments; push_back takes an existing object (moving or copying it) Correct
  • C push_back is always faster
  • D emplace_back sorts the vector
Correct Answer: Option B

With move semantics the difference is often small, but emplace avoids the temporary.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links