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 5997771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:18:41+00:00 2026-05-23T00:18:41+00:00

#include <iostream> #include <vector> using namespace std; class base { int x; public: base(int

  • 0
#include <iostream>
#include <vector>
using namespace std;
class base
{
    int x;
public:
    base(int k){x =k; }
    void display()
    {
        cout<<x<<endl;
    }

    base(const base&)
    {
        cout<<"base copy constructor:"<<endl;
    }
};
int main()
{
    vector<base> v;
    base obase[5]={4,14,19,24,29};
    for(int i=0; i<5; i++)
    {
        v.push_back(obase[i]);
    }

}

When data is inserted into vector, copy to that data goes to vector using the copy constructor.

When i run this program,

  1. for the first insertion (i=0), one time copy constructor is called.
  2. for the second insertion (i=1), two times copy constructor is called
  3. for the third insertion (i=3), three times copy constructor is called
  4. for the fourth insertion (i=3), four times copy constructor is called
  5. for the fifth insertion (i=4), five times copy constructor is called

Please any one can tell me why this is happening? For each insertion, shouldn’t the copy constructor be called only once?

  • 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-23T00:18:42+00:00Added an answer on May 23, 2026 at 12:18 am

    If v needs to resize its internal buffer, it will usually allocate a totally fresh memory area, so it needs to copy all the objects that were previously in the vector to the new location. This is done using regular copying, so the copy constructor is invoked.

    You should call reserve() on the vector to reserve storage upfront if you can estimate how many elements you are going to need.

    Note that the resize/growth behaviour of std::vector is implementation-dependent, so your code sample will produce different results with different standard library implementations.

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

Sidebar

Related Questions

#include <iostream> #include <vector> using namespace std; class Base { public: void Display( void
#include <iostream> using namespace std; class Base { public: Base(){cout <<Base<<endl;} virtual ~Base(){cout<<~Base<<endl;} virtual
#include<iostream> using namespace std; class A { int a; int b; public: void eat()
#include <boost/ptr_container/ptr_vector.hpp> #include <iostream> using namespace std; class Derived { public: int i; Derived()
#include <iostream> #include <vector> using namespace std; typedef int UINT4; class Hack {}; Hack&
#include <iostream> #include <vector> #include <algorithm> using namespace std; class A { public :
#include <iostream> #include <vector> using namespace std; int main() { vector< vector<int> > dp(50000,
I have the following code #include <iostream> #include <vector> using namespace std; int distance(vector<int>&
#include <iostream> #include <vector> using namespace std; struct s_Astruct { vector <int> z; };
#include iostream #include vector class ABC { }; class VecTest { std::vector<ABC> vec; public:

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.