Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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:</p> <pre><code>for(int i=1;i&lt;112;i++){ n_2.push_back(vector&lt;struct nodo&gt;(112-i)); for(int j=1;j&lt;112-i;j++){ n_2[i][j].last_prod=j; } } </code></pre> <p>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. </p> <p>Finally, if your array is a constant size, consider using <a href="http://en.cppreference.com/w/cpp/container/array" rel="nofollow">std::array</a> rather than std::vector. Note that std::array is a C++11 feature and may not be available depending on your compiler.</p> <p>If I were writing this code, I'd probably write it like this:</p> <pre><code>#include &lt;array&gt; using namespace std; // allocate an array of 112 &lt;struct nodo&gt; arrays, each of size 112 array&lt;array&lt;struct nodo, 112&gt;, 112&gt; n_2; for (int i = 0; i &lt; 112; i++) { for (int j = 0; j &lt; 112; j++) { n_2[i][j].last_prod = j; } } </code></pre> <p>Or alternatively, if I don't have a compiler that supports C++11:</p> <pre><code>#include &lt;vector&gt; using namespace std; // allocate a vector of size 112 of &lt;struct nodo&gt; vectors, each of size 112 vector&lt;vector&lt;struct nodo&gt; &gt; n_2(112, vector&lt;struct nodo&gt;(112)); for (int i = 0; i &lt; 112; i++) { for (int j = 0; j &lt; 112; j++) { n_2[i][j].last_prod = j; } } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload