I want to check neighborhoods of cell in two dimensional array in C++ I want to write a function which will return all possible/valid 'X' moves around my Target cell depending on it position inside the following two dimensional array. 

X X X
X T X
X X X


But my 2 concerns are:


the performance because my "game field" will be 1000x1000 cells and each cell must be checked in the game turn. So creating 1,000,000 vectors (in one turn) is a bad idea? Maybe I should use a different data structure for it? 
logic inside the function is not too complicated?

enum dir {topL, top, topR, l, r, botL, bot, botR};

std::vector&lt;dir&gt; neighborhood(int x, int y, int size)
{
    std::vector&lt;dir&gt; direction;
    direction.clear();

    if(x &lt; size -1){direction.push_back(bot);}
    if(y &lt; size -1){direction.push_back(r);}
    if(x &lt; size -1 &amp;&amp; y &lt; size -1){direction.push_back(botR);}
    if(x &gt; 0){direction.push_back(top);}
    if(y &gt; 0){direction.push_back(l);}
    if(x &lt; size -1 &amp;&amp; y != 0){direction.push_back(botL);}
    if(x &gt; 0 &amp;&amp; y &lt; size -1){direction.push_back(topR);}
    if(x &gt; 0 &amp;&amp; y &gt; 0){direction.push_back(topL);}
    return direction;
}
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.