3

Previously, I've been creating 2D arrays of class Object using the pointer, in the following syntax:

Object** myArray = new Object*[row_num];
for (int i = 0; i < row_num; i++)
{
    myArray[i] = new Object[col_num];
    [skip]
}

However, many people have been recommending me to use vector<vector<Object>> rather than using Object**.

As far as I'm concerned, vector requires more memories as a trade-off for easier change in the array size. However, since the 2D array that I need is used for a backtracking algorithm of a grid (which would never change its dimension once it's determined), I do not feel the necessity of changing them.

Are there any other advantages of vector that I'm unaware of?

Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88
Caladbolgll
  • 77
  • 1
  • 6
  • 3
    One advantage is you never need to remember to call `delete` – NathanOliver Jul 21 '15 at 19:49
  • 2
    Another would be that as long as you use the `at` function you can greatly reduce your chances of undefined behavior. Also see [here](http://stackoverflow.com/questions/15079057/arrays-vs-vectors-introductory-similarities-and-differences) – scohe001 Jul 21 '15 at 19:52
  • `vector> ` is bad if the inner vector has a constant size. Having that, encapsulate a vector in some class and access elements via operator () (size_t row, size_t column) –  Jul 21 '15 at 19:55
  • Even your current method is really not as optimal as it can be. Given that you stated that your array doesn't change dimensions, your implementation can be optimized by making only 2 calls to `new` (and then only 2 calls to `delete`), regardless of the number of rows. http://stackoverflow.com/questions/21943621/how-to-create-a-contiguous-2d-array-in-c/21944048#21944048 – PaulMcKenzie Jul 21 '15 at 20:01
  • 1
    Also, don't be distracted by any claim that `vector` "takes up more memory". That's essentially false, the extra memory involved will be tiny and you'll never notice it. On the other hand, there are countless brilliant advantages to `vector`. In fact, you'll probably write faster (and possibly more memory-efficient) code using `vector`, because you'll be able to understand the big picture of your algorithm and (safely) push towards better algorithms. – Aaron McDaid Jul 21 '15 at 20:08

2 Answers2

4

There are a few advantages of using vectors over raw arrays. First you don't have to remember to delete anything as the vector takes care of that when it goes out of scope. Secondly a vector has a lot a built in member functions that mike life easier like size() and at(). Third you can use ranged based for loops with them and you can write code like:

// print out all elements in a jagged 2d structure
std::vector<std::vector<int>> data;
//  load data into data
for (const auto & row : data)
    for (const auto & elem : row)
        //print out elem

Which is very clean looking and understandable.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
4

Automatic deallocation

As @NathanOliver mentioned, you never need to call delete (often a customized recursive deletion) for multi-dimensional vectors, as the objects inside will be released when they fall out of scope automatically. This can significantly reduce the amount of code you need to write and maintain.

Obviously, if your vectors contain objects that were allocated with new or malloc, you'll need to delete them normally. This is where shared_ptrs come in but that's another topic.

Regarding overhead

While there is a slight overhead for each vector, it is far more in line with the C++ paradigm to use them over traditional C-style arrays. If your vector is of a fixed size, you can also use std::array<T, N>, and avoid a lot of the overhead that having a dynamically sized container brings.

C++-ness

Consistency in language is important. If you're going to be writing C++ using the standard library, you should stick to that as closely as is possible. I've worked at many places that use random hodge-podge of C and C++ mixed together, and it makes reading and understanding the code a nightmare.

Colin Basnett
  • 4,052
  • 2
  • 30
  • 49