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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:20:49+00:00 2026-05-18T23:20:49+00:00

I have some std::vector and I have to resize it with some default value.

  • 0

I have some std::vector and I have to resize it with some default value. Here is the code:

static int Counter = 0;
class Data
{
   /* ... */
   Data() { 
      Counter++; 
      std::cout << Counter << std::endl;
   }
};

std::vector<Data> mArray;

for (int i=0; i <= 200; ++i)
{
   mArray.push_back(Data());
}

// And resizing somewhere:
std::cout << "Resizing!\n";
mArray.resize(400, Data());

As I understand, after inserting 200 items, I can resize it with resize function which takes new size and default value for each new element.

When I run that program I see:

0
1
2
...
199
200
Resizing
201

Why does only 1 item is inserted after resizing?

  • 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-18T23:20:50+00:00Added an answer on May 18, 2026 at 11:20 pm

    You’re only seeing a count from your default constructor, when the entries being added are copy constructed. You’ll have to add a copy constructor that counts copies as well:

       Data(const Data& other) { 
          // Actual copying code, whatever that may be
          Counter++; 
          std::cout << Counter << std::endl;
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some methods like this in my class: static std::string numberToString(int n); static
I have some numbers stored in a std::vector<int> . I want to find which
I have some code that looks like: static const std::string and( AND ); This
I have some code that adds to a std::vector and a std::map after creating
I have a vector object: std::vector<std::vector<MyClass>> _matrix; It is 2d array with some data.
I have some vectors of class A objects: std::vector<A> *V1; std::vector<A> *V2; etc there
Currently, I have some code as follows template<typename Type> Type* getValue(std::string name, bool tryUseGetter
I'm porting some C++ code to C#. Does C# have an equivalent to std::nth_element()
I have some data that is stored in a sorted vector. This vector is
I have the following code, which does some iterator arithmetic: template<class Iterator> void Foo(Iterator

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.