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

  • SEARCH
  • Home
  • 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 496403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:40:59+00:00 2026-05-13T05:40:59+00:00

When invoking the method push_back from std::vector , its size is incremented by one,

  • 0

When invoking the method push_back from std::vector, its size is incremented by one, implying in the creation of a new instance, and then the parameter you pass will be copied into this recently created element, right? Example:

myVector.push_back(MyVectorElement());

Well then, if I want to increase the size of the vector with an element simply using its default values, wouldn’t it be better to use the resize method instead? I mean like this:

myVector.resize(myVector.size() + 1);

As far as I can see, this would accomplish exactly the same thing but would avoid the totally unnecessary assignment copy of the attributes of the element.

Is this reasoning correct or am I missing something?

  • 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-05-13T05:40:59+00:00Added an answer on May 13, 2026 at 5:40 am

    At least with GCC, it doesn’t matter which you use (Results below). However, if you get to the point where you are having to worry about it, you should be using pointers or (even better) some form of smart pointers.. I would of course recommend the ones in the boost library.

    If you wanted to know which was better to use in practice, I would suggest either push_back or reserve as resize will resize the vector every time it is called unless it is the same size as the requested size. push_back and reserve will only resize the vector if needed. This is a good thing as if you want to resize the vector to size+1, it may already be at size+20, so calling resize would not provide any benefit.

    Test Code

    #include <iostream>
    #include <vector>
    
    class Elem{
        public:
            Elem(){
                std::cout << "Construct\n";
            }
            Elem(const Elem& e){
                std::cout << "Copy\n";
            }
            ~Elem(){
                std::cout << "Destruct\n";
            }   
    };
    
    
    int main(int argc, char* argv[]){
        {
            std::cout << "1\n";
            std::vector<Elem> v;
            v.push_back(Elem());
        }
    
        {
            std::cout << "\n2\n";
            std::vector<Elem> v;
            v.resize(v.size()+1);
        }
    }
    

    Test Output

    1
    Construct
    Copy
    Destruct
    Destruct
    
    2
    Construct
    Copy
    Destruct
    Destruct
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I want to throw a new exception when invoking a generic method with
why invoking a method by reflection is much slower than making a interface then
I am invoking a method from within an AOP (Spring) (Around advice). Once the
I am using class.forname to create a new instance of class.The classname comes from
I'm loading a Groovy script/class from a remote server, creating a new instance, and
I have a method invoking bean which calls a method to perform some sort
I know this represents the object invoking the method and static methods are not
I am having a function as follows: public int CreateMyTask() { Console.WriteLine(Invoking CreateTask method);
I'm invoking UIImagePickerController's method takePicture method to programmatically pictures with the iPhone However I
I am invoking camera from my application. i want to remove few menu items

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.