0

It seems that in general, vectors are to be preferred over lists, see for example here, when appending simple types.

What if I want to fill a matrix with simple types? Every vector is a column, so I am going to go through the outer vector, and append 1 item to each vector, repeatedly.

Do the latter vectors of the outer vector always have to be moved when the previous vectors increase their reserved space? As in is the whole data in one continuous space? Or do the vectors all just hold a pointer to their individual memory regions, so the outer vector's memory size remains unchanged even as the individual vectors grow?

Community
  • 1
  • 1
Cookie
  • 12,004
  • 13
  • 54
  • 83
  • No they don't have to be moved. Multi dimenional dynamic arrays are held in non-contiguous space. – kkuryllo May 03 '14 at 11:44
  • 1
    "Or do the vectors all just hold a pointer to their individual memory regions, so the outer vector's memory size remains unchanged even as the individual vectors grow?". Exactly – deviantfan May 03 '14 at 11:45

2 Answers2

0

Taken from the comments, it appears vectors of vectors can happily be used.

Cookie
  • 12,004
  • 13
  • 54
  • 83
  • If I could give a piece of advice. Don't worry about the efficiency of the vector class recopying data until you run into problems with you code running slow. Copying of contiguous data is extremely fast. I've recently been converting many of my 2D vectors to 1D vectors inside of a wrapper class to force them to be contiguous for performance gains. Unless you're constantly inserting/erasing from the internal regions of massive vectors it's unlikely to have any negative impact. – kkuryllo May 03 '14 at 11:54
  • Yes, vectors of vectors are fine. The vector of vectors will (most likely) only grow if you append another column, ie. another vector. –  May 03 '14 at 12:08
0

For small to medium applications, the efficiency of the vectors will seldom be anything to worry about.

There a couple of cases where you might worry, but they will be uncommon.

class CData {};  // define this
typedef std::vector<CData> Column;
typedef std::vector<Column> Table;

Table tab;

To add a new row, you will append an item to every column. In a worst case, you might cause a reallocation of each column. That could be a problem if CData is extremely complex and the columns currently hold a very large number of CData cells (I'd say 10s of thousands, at least)

Similarly, if you add a new column and force the Table vector to reallocate, it might have to copy each column and again, for very large data sets, that might be a bit slow.

Note, however, that a new-ish compiler will probably be able to move the columns from the old table to the new (rather than copying them), making that trivially fast.

As @kkuryllo said in a comment, it generally isn't anything to worry about.

Work on making your code as clean, simple and correct as possible. Only if profiling reveals a performance problem should you worry about optimising for speed.

Michael J
  • 7,631
  • 2
  • 24
  • 30