Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It depends very much on <strong>what kind of operations</strong> you are going to perform on that structure. Will you have to do <em>searches</em>? If so, what kind: value-based or position-based? Will you have to perform <em>transformations</em> on it? Will you have to access all of its elements repeatedly and in a sequence? Will you have to access elements based on their <em>position</em> in the grid? Is your structure <em>sparse</em> or <em>dense</em>?</p> <p>Moreover, do you want to minimize <em>creation</em> time, <em>search</em> time, <em>navigation</em> time, or <em>transformation</em> time? This all gives fundamental information to make a decision.</p> <p>I would <strong>not</strong> go for a solution based on nodes and links, because it is:</p> <ol> <li>expensive in terms of <em>memory consumption</em> (due to the extra link pointers);</li> <li>expensive in terms of <em>complexity</em> (navigating all the nodes require following all the links, so no random access), and;</li> <li>expensive in terms of <em>data locality</em> (nodes will be allocated individually and spread all over the heap at separate addresses, which will generate many cache misses and slow down your program);</li> <li><em>error-prone</em> (messing up with links is easy, especially if you use raw pointers and don't want to pay for smart pointers' memory overhead)</li> </ol> <p>Without knowing too much about your requirements, I would probably suggest you having a look at <a href="http://www.boost.org/doc/libs/1_52_0/libs/multi_array/doc/user.html" rel="noreferrer">Boost.MultiArray</a>.</p> <p>If you want to do things on your own, then (again, without knowing too much about your requirements) I would suggest you using a <code>vector&lt;vector&lt;vector&lt;double&gt;&gt;&gt;</code>, which is relatively simple, does not have fixed size and allows you resizing at run-time, guarantees you O(1) access through the subscripting operator and also <em>some</em> locality of the data (you have several vectors here, so as long as you access data from <em>one</em> vector performance will be fine).</p> <p>A plain C-style multi-dimensional array is also a possibility, but it's inherently unsafe, which would make it a last-resort choice if I were you (also, allocating a contiguous block of memory might be impossible if your structure is huge).</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