title: Find the adjacent neighbors of two-dimensional vector with respect to boundaries/limit of the vector
body: <p>I've created a two-dimensional vector of strings in C++ and it currently takes a "map" of different characters (e.g. R, I, -, etc) to represent different "zones" on the map. The program needs to be able to detect if the map has certain characters and if it does, it needs to check all the adjacent spots around it. Below is an example of a map that can be used. Note that the size of the vector is not static and can change depending on the map selected and it's size during the start of the program.</p>
  
  <pre><code> , ,T,#,T,T,T,
  I,I,I,-,C,C,T,
  I,I,I,-,C,C,T,P
  I,I,I,-,C,C,T,
  -,-,-,-,-,-,#,-
   , ,-,R,R,R,T,
   , ,-,R,R,R, ,
   , ,-,R,R,R, ,
  </code></pre>
  
  <p>Let's say, for instance, I'm detecting if the map has character 'R' inside it. I've found an instance of 'R' at coordinate (5,3) and now I need to check all adjacent spots around it with respect to the boundaries of the vector; if the coordinate the program is currently at is a corner (e.g. 0,0) then it only has to check 3 adjacent spots whereas a coordinate of (5,3) has 8 adjacent spots to check. </p>
  
  <p>I want to write a loop that automatically detects the boundaries of the vector so that when the program is checking the adjacent spots, it doesn't try to check spots that don't exist. So for a coordinate (0,0) that only has 3 adjacent spots, the program would only check spots (0,1),(1,1),(1,0) whereas a spot with  8 adjacent spots (5,3), it would only check spots (4,2),(4,3),(4,4),(5,2),(5,4),(6,2),(6,3),(6,4). Checking spots that don't exist causes a segmentation fault within my compiler. I'm unsure of how to write the loop to check adjacent spots of my vector within respect to its boundaries. Does anyone have any recommendations for how to write the loop?</p>