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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:50:08+00:00 2026-05-30T01:50:08+00:00

I need to set up and access a two dimensional vector of structure in

  • 0

I need to set up and access a two dimensional vector of structure in C++.
My structure is defined as:

struct nodo{
  int last_prod;
  int last_slot;
  float Z_L;
  float Z_U;
  float g;
  bool fathomed;
};

I defined the vector as:

vector<vector<struct nodo> > n_2;

Now,I need to create several elements of n_2, which will then be vectors again, and then access the single members of them.
How can I achieve that? that is the piece of code I have so far:

for(int i=1;i<112;i++){
    n_2.push_back(vector<struct nodo>(111-i));       
    for(int j=1;j<112-i;j++){
      n_2[i][j].last_prod=j;
    }
}

which doesn’t work.

  • 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-30T01:50:08+00:00Added an answer on May 30, 2026 at 1:50 am

    A vector is size 0 until you tell it to resize, or unless you initialize it with a specific size. Pass the size of your vector in when you create it:

    for(int i=1;i<112;i++){
        n_2.push_back(vector<struct nodo>(112-i));       
        for(int j=1;j<112-i;j++){
          n_2[i][j].last_prod=j;
        }
    }
    

    Also, it looks like you are skipping over the 0th index, which means your first value in your array will be skipped. That is probably undesired.

    Finally, if your array is a constant size, consider using std::array rather than std::vector. Note that std::array is a C++11 feature and may not be available depending on your compiler.

    If I were writing this code, I’d probably write it like this:

    #include <array>
    using namespace std;
    
    // allocate an array of 112 <struct nodo> arrays, each of size 112
    array<array<struct nodo, 112>, 112> n_2;
    for (int i = 0; i < 112; i++)
    {
        for (int j = 0; j < 112; j++)
        {
            n_2[i][j].last_prod = j;
        }
    }
    

    Or alternatively, if I don’t have a compiler that supports C++11:

    #include <vector>
    using namespace std;
    
    // allocate a vector of size 112 of <struct nodo> vectors, each of size 112
    vector<vector<struct nodo> > n_2(112, vector<struct nodo>(112));
    for (int i = 0; i < 112; i++)
    {
        for (int j = 0; j < 112; j++)
        {
            n_2[i][j].last_prod = j;
        }
    }
    

    Even more ideally, you should use a 1 dimensional vector, and simply treat it as a 2 dimensional vector. This way, you can do a single allocation of memory all at once rather than 112 smaller allocations. This is getting pretty nitpicky but obviously a O(1) solution is better than an O(n) solution which is better than a O(n^2) solution in terms of allocations since allocations are slow.

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

Sidebar

Related Questions

Do we need to set permission to access the server with username password authentication
I need to retrieve a set of Widgets from my data access layer, grouped
I have a set of different interfaces and I need to give them access
I need to know how to access a session set by one website or
I need to perform an exponential operation of two parameters (one set: t, and
Need set zoom for camera preview...
I need Set collection, where its items will be identified by items class. Something
I need to set the height of every textbox on my form, some of
I need to set the text within a DIV element dynamically. What is the
I need to set up an instance of SQL Server 2005 with SQL_Latin1_General_CP850_Bin as

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.