vector - C++ What is the best data structure for a two dimensional array of object "piles"? -


the size of grid known @ start (but different each time program starts). however, depth of each cell not mere value, rather population of objects vary during runtime.

q: recommended (efficient , easy maintain; less prone user error) way of implementing ?

  • is kind of standard 2d array of vector pointers ?
  • is 3d vector array ?
  • is 2d array of linked lists, or binary trees (i thinking binary trees add complexity overhead because of continuous deletion , insertion node-gymnastics)
  • is other custom data structure ?

enter image description here

use 1d array best cache locality. vector fine this.

std::vector<int> histdata( width * height ); 

if need index rows quickly, make point it:

std::vector<int*> histogram( height ); histogram[0] = &histdata[0]; for( int = 1; < height; i++ ) {     histogram[i] = histogram[i-1] + width; } 

now have 2d histogram stored in 1d vector. can access this:

histogram[row][col]++; 

if wrap in simple class, you're less silly pointers. can make clear() function set histogram data 0 (which rips through histdata vector , zeros it).


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -