I want to check neighborhoods of cell in two dimensional array in C++
You can use 8 bit memory (char in c/c++) to precompute possible direction of movement for each of the cells and use bitwise operation to improve performance:

//bit1: top direction, bit2: topright directon, bit3: rigth, and so on....
std::array&lt;char, 1000000&gt; c;    //array, this will be our playground
c.assign(255);                  //to set all ones in all cells
c[0] = c[0] &amp; 0x38;             //top left cell representing 00111000
c[999] = c[999] &amp; 0xE;          //top right cell
c[900000] = c[900000] &amp; 0xE0;   //bottom left cell
c[999999] = c[999999] &amp; 0x83;   //bottom right cell

for(int i=1; i&lt;999; i++)               //all top edge cells except corners
   c[i] &amp;= 0x3E;
for(int i=1999; i&lt;999999; i += 1000)   //all right edge cells except corners
   c[i] &amp;= 0x8F
for(int i=9999000; i&lt;999999; i ++)     //all bottom edge cells except corners
   c[i] &amp;= 0xE3
for(int i=1000; i&lt;999000; i += 1000)   //all left edge cells except corners
   c[i] &amp;= 0xF8


then you can use these hex values to quickly determine if your cell is at corners, edges or in the middle
    example: 

  if(! c[i] ^ 0x38){
       //this cell is left corner
    }


Because bitwise operators are very fast, you will see great improvement in performance.