container vs built-in array

Hey,

As far as i know it's more preferred to use containers than the built-in arrays.
I've always followed the advice but how about performance between these both with a custom type?

1
2
3
4
5
6
static const int NUM_RANGE = 40;
static const int NUM_RANGE2 = 10;


MyTime my_array[NUM_RANGE];
std::vector<MyType> my_vector(NUM_RANGE);


and what about 2D arrays/containers:

1
2
MyType arr[NUM_RANGE][NUM_RANGE2];
std::vector< std::vector< MyType > > vec ( NUM_RANGE, std::vector<MyType> ( NUM_RANGE2 ) );


What about performance? is the container(vector) still preferred?


Last edited on
If the size is fixed you can use std::array which will have no overhead compared to a built in array but often makes life much easier.

http://www.cplusplus.com/reference/array/array/

Personally I don't like using vectors of vectors unless I want each vector to have it's own size. If I'm doing a grid (like a game map) where all rows have the same size I prefer using a single vector and index the elements as y * width + x.

1
2
3
4
5
6
7
8
9
10
std::vector<Tile> map(width * height);

for (int y = 0; y < height; ++y)
{
	for (int x = 0; x < width; ++x)
	{
		Tile& tile = map[y * width + x];
		// Do stuff with tile ...
	}
}

In the above example I used x and y just to demonstrate but if you don't need x and y for your computation you can instead iterate all tiles using a single range-based for loop. Note that the way I iterate goes through each element in the order they are stored in the vector, so it doesn't jump around in memory, making it more cache friendly.

If the size is fixed you could use the same technique on an std::array but the performance benefits are not as great because nested std::arrays is just one big memory allocation. Only downside I can think of using nested std::array in this case would be that you would always be forced to iterate using nested loops.
Last edited on
hmm, for 2D arrays i've always used the build-in array

Type grid[x][y];

for me it's somehow also more readable.
---

so i should, always prefer available containers and actually never use the built-in arrays?
Last edited on
If you want you can always create a wrapper class for your 2D array to hide the ugly details.

If you work with nested containers I think you should at least use typedefs so that you don't have to write std::vector<... all the time.
1
2
3
4
typedef std::vector<Tile> Map;

Map map(width * height);
// do things with map... 

or, if you want to use nested std::arrays:
1
2
3
4
5
typedef std::array<Tile, WIDTH> Row;
typedef std::array<Row, HEIGHT> Map;

Map map;
// do things with map... 


so i should, always prefer available containers and actually never use the built-in arrays?

Well, I think so yes, but I have to admit I still use built in arrays sometimes. I guess I have not yet got used to using std::array because it was only recently added to C++. Sometimes built in arrays are less verbose. The advantage of std::array compared to built-in arrays is that you can pass, return, copy and in many other ways use them like other containers. With built in arrays there is no simple way to create a copy, and you can't simply return them from functions (not the normal way at least).
Last edited on
ok thanks :)
As far as i know it's more preferred to use containers than the built-in arrays.

It depends on your design .
It depends on your design .
With static/automatic arrays std::array is not worse than builtin array and has numerous advantages.
With dynamic arrays std::vector is no worse than builtin array and has numerous advantages.
Therefore you should almost always prefer container to builtin arrays.
Topic archived. No new replies allowed.