0

I have got a class that represents a 2D map with size 40x40.

I read some data from sensors and create this map with marking cells if my sensors found something and I set value of propablity of finding an obstacle. For example when I am find some obstacle in cell [52,22] I add to its value for example to 10 and add to surrounded cells value 5.

So each cell of this map should keep some little value(propably not bigger). So when a cell is marked three times by sensor, its value will be 30 and surronding cells will have 15.

And my question is, is it worth to use casual array or is it better to use vector even I do not sort this cells, dont remove them etc. I just set its value, and read it later?

Update: Actually I have in my header file:

using cell = uint8_t;

class Grid {
private:
    int xSize, ySize;
    cell *cells;
public: 
    //some methods
}

In cpp :

using cell = uint8_t;

Grid::Grid(int xSize, int ySize) : xSize(xSize), ySize(ySize) {
    cells = new cell[xSize * ySize];
    for (int i = 0; i < xSize; i++) {
        for (int j = 0; j < ySize; j++)
            cells[x + y * xSize] = 0;
    }
}

Grid::~Grid(void) {
    delete cells;
}

inline cell* Grid::getCell(int x, int y) const{
    return &cells[x + y * xSize];
}

Does it look fine?

Mateusz
  • 604
  • 8
  • 20

3 Answers3

4

I'd use std::array rather than std::vector.

For fixed size arrays you get the benefits of STL containers with the performance of 'naked' arrays.

http://en.cppreference.com/w/cpp/container/array

Roddy
  • 66,617
  • 42
  • 165
  • 277
  • so in this case is it better to use std::array than vector or new short[xSize * ySize]; ? – Mateusz Mar 11 '16 at 16:17
  • 2
    @Mateusz: Reasonable people may differ here in the choice between vector and array, but they all avoid `new [ ]`. – MSalters Mar 11 '16 at 16:26
  • @Mateusz a `short` is not short enough in your case, as it's often 2-bytes, and you need only one. But that's not a huge deal. The important thing is you should not need to write a single `new` or `delete` in your code, except in memory managing classes. Use containers (vector, maps, etc...) instead. It save a lot of troubles. – johan d Mar 11 '16 at 16:50
2

A static (C-style) array is possible in your case since the size in known at compile-time.

BUT. It may be interesting to have the data on the heap instead of the stack.

  • If the array is a global variable, it's ugly an bug-prone (avoid that when you can).
  • If the array is a local variable (let say, in your main() function), then a stack overflow may occur. Well, it's very unlikely for a 40*40 array of tiny things, but I'd prefer have my data on the heap, to keep things safe, clean, and future-proof.

So, IMHO you should definitely go for the vector, it's fast, clean and readable, and you don't have to worry about stack overflow, memory allocation, etc.

About your data. If you know your values are storable on a single byte, go for it ! An uint8_t (same as unsigned char) can store values from 0 to 255. If it's enough, use it.

using cell = uint8_t; // define a nice name for your data type
std::vector<cell> myMap;
size_t size = 40;
myMap.reserve(size*size);

side note: don't use new[]. Well, you can, but it has no advantages over a vector. You will probably only gain headaches handling memory manually.

johan d
  • 2,798
  • 18
  • 26
0

Some advantages of using a std::vector is that it can be dynamically allocated (flexible size, can be resized during execution, etc) and can be passed/returned from a function. Since you have a fixed size 40x40 and you know you have one element int in every cell, I don't think it matters that much in your case and I would NOT suggest using a class object std::vector to process this simple task.

And here is a possible duplicate.

Community
  • 1
  • 1
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104