Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8257159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:06:19+00:00 2026-06-08T02:06:19+00:00

Suppose I have the following code: #include <vector> struct A { int a; int

  • 0

Suppose I have the following code:

#include <vector>
struct A {
    int a;
    int x;
};
int main() {
    using namespace std;
    A a1;
    A a2;
    vector<A> va;
    va.push_back(a1);
    va.push_back(move(a2));
}

I am aware that the elements of std::vector are stored contiguously, unlike a std::list. In the above code a2 is moved but is there really no copying of a2 to the vector va? What is the difference between va.push_back(a2); and va.push_back(move(a2));?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:06:20+00:00Added an answer on June 8, 2026 at 2:06 am

    In your case, there is no effective difference, since you are using compiler-provided copy constructors. You would see a noticeable performance difference when using objects that are move-constructible, and take a lot of effort to copy. In that case, using push_back(x) would create a copy of the object, while push_back(move(x)) would tell push_back() that it may “steal” the contents of x, leaving x in an unusable and undefined state.

    Consider if you had a vector of lists (std::vector<std::list<int> >) and you wanted to push a list containing 100,000 elements. Without move(), the entire list structure and all 100,000 elements will be copied. With move(), some pointers and other small bits of data get shuffled around, and that’s about it. This will be lots faster, and will require less overall memory consumption.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose i have a php file named main.php It Contains following code include 'config.php';
Suppose we have the following code: #include <iostream> struct A { virtual void f()
I have the following code: #include <irrlicht.h> using namespace irr; using namespace core; using
Suppose I have following code package memoryleak; public class MemoryLeak { public static int
Suppose I have the following: using(var ctx = DataContextFactory.Create(0)) { ... Some code ...
Suppose I have the following files: lib/A.h #ifndef A_H #define A_H #include <vector> class
Suppose that we have the following bit of code: #include <pthread.h> #include <stdio.h> #include
Have a look at the following code: #include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<string.h> #include<sys/types.h> main() {
Possible Duplicate: Are file descriptors shared when fork()ing? Suppose I have following code in
Suppose I have the following code to collect all the possible combinations. abArray =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.