Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ dynamic (multi-stage) 3D vector instantiation and element assignment
    text
    copied!<p>Okay, so I need to create a 3D data-structure at run-time, I decided to use std::vector, the problem is as follows: I know the dimension of the 1st dimension at instantiation time (when I create the object I'm using it in), but I don't know the dimension of the second until run-time, and the size of 3rd dimensions can vary. <strong>I have created the 3D vector</strong> and the run-time doesn't complain, however I'm having difficulty <strong>assigning values</strong> to the elements.</p> <p>This code is part of an object I'm creating. In the class definition I have:</p> <pre><code>std::vector&lt; std::vector&lt; std::vector&lt;double&gt; &gt; &gt; splits; </code></pre> <p>Then in the object constructor, in order to create/allocate the first dimension, I have:</p> <pre><code>for(int i=0; i&lt;sizeOfDimOne; i++){ //create 1st dimension splits.push_back( std::vector&lt; std::vector&lt;double&gt; &gt;() ); } </code></pre> <p>based on user input I need to create 2nd dimension of a certain size, I call a method for this:</p> <pre><code>for(int i=0; i&lt;sizeOfDimOne; i++){ for(int j=0; j&lt;sizeOfDimTwo; j++) //create second dimension splits[i].push_back( std::vector&lt;double&gt;() ); } </code></pre> <p>However, when I get to assigning values:</p> <pre><code>for(int i=0; i&lt;sizeOfDimThree; i++){ splits[dim1][dim2].push_back( someValue ); //adding an element to the 3rd dim } </code></pre> <p>(The <em>someValue</em> changes constantly (and is a double), and you don't have to worry about lower dimension indexing, everything checks out.) The problem is - when I checked <em>splits[dim1][dim2][i]</em> the value was <strong><em>0.0</em></strong> for (presumably all) entries, naturally this is not what was provided by <em>someValue</em>.</p> <p>I have also tried creating the 3rd dimension using <em>.resize(sizeOfDimThree)</em> and then assigning using</p> <pre><code>splits[dim1][dim2][i] = whatever; </code></pre> <p>but that didn't work at all - no object/element was created at all.</p> <p>I realize this perhaps isn't the most straight forward manner of phrasing the question, but I believe it is the most accurate, as there might be multiple-points of failure.</p>
 

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