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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:13:21+00:00 2026-05-17T01:13:21+00:00

I have a vector object: std::vector<std::vector<MyClass>> _matrix; It is 2d array with some data.

  • 0

I have a vector object:

std::vector<std::vector<MyClass>> _matrix;

It is 2d array with some data.
When i trying to resize the dimensions with:

_matrix.resize(_rows, std::vector<MyReal>(_colms)); //_rows and _colms are ints

this command simply does nothing to the object.
So to resize it i have to call first to:

_matrix.clear();

and then:

_matrix.resize(_rows, std::vector<MyReal>(_colms)); 

Of course, I’m losing the data. (In my case it doesn’t matter)

Is this expected behaviour?

  • 1 1 Answer
  • 1 View
  • 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-17T01:13:22+00:00Added an answer on May 17, 2026 at 1:13 am

    From the docs for vector::resize:

    _Val: The value of new elements added to the vector if the new size is larger that the original size.

    Only the new rows get vectors with additional columns (std::vector<MyReal>(_colms)). resize will not change the existing rows.

    Update: To resize the entire vector properly, iterate over the existing rows and resize those vectors, then add the new rows. Something like this should work:

    for (size_t i = 0; i < _matrix.size(); i++)
      _matrix[i].resize(_colms);
    _matrix.resize(_rows, std::vector<MyReal>(_colms));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector object that contains clipboard data. I am trying to write
I have a C++ std::vector denoted as: std::vector<GameObject*> vectorToSort; Each object in vectorToSort contains
I have the following code: void Foo() { static std::vector<int>(3); // Vector object is
I have some code that adds to a std::vector and a std::map after creating
I have a 3D vector defined like this... std::vector<std::vector<std::list<Object*> > > m_objectTiles; I have
I have a question about converting from std::vector to an array. I have a
I have a vector populated with objects: std::vector<Stuff*> stuffVector; and am trying to delete
I have a question regarding vectors: If I have a std::vector<MyClass> will this vector
I have an std::vector of objects and i cycle it calling some methods of
So I have a std::vector<std::unique_ptr<Base>> vec and I'm trying to sort it dynamically, given

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.