Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If the whole matrix has a mostly constant width and height, you may as well use a single <code>vector</code>, and address cells with <code>(row * columnCount) + column</code>. That way the whole thing will be stored in a single memory block instead of in several fragmented blocks for each row. (Though of course you are doing the right thing to wrap this concept up in a new class - I'm just talking about the behind-the-scenes implementation.)</p> <p>A vector of vectors has the unfortunate property that if you insert a row at the top, <code>std::vector</code> will perform a copy construction (or assignment, possibly) for all the other rows as it shifts them down by one place. This in turn involves reallocating the storage for every row and individually copying the items in the cells of every row. (C++0x will probably be better at this.)</p> <p>If you know that you will be doing that kind of thing often, the advantage of a single large memory block is that you can insert a new row at the top and <code>std::vector</code> will only have to shift all the cells forward by <code>columnCount</code> places, so it will seriously reduce the number of heap operations (freeing/reallocating of individual blocks).</p> <p>Although as you suggest, a vector of pointers to vectors would have the further advantage that it would only need to shift forward a lot of pointer values, and the size of the block containing all the row pointers will be much smaller, further lessening the impact of heap operations.</p> <p>Of course, the only way to be sure of the actual impact of these things on the performance of an application is to time it with various implementations and compare them. This is why you're doing exactly the right thing by hiding these details inside a new class.</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