How do I check if a position in my vector of vectors is out of bounds?









up vote
0
down vote

favorite












I have a vector of vectors filled with characters from a text file. It is essentially a simple outbreak simulator, with 'i' characters being infected, and 's' characters being susceptible to infection. The point is to run through the matrix and if it comes across an 'i', it then changes all 's' around it into an 'i'. I run into a problem when checking the elements around it due to checking positions out of the bounds on the edges of the matrix. Is there a way to check these bounds in my if statements?



Here is the code:



for (int i = 0; i < population.size(); i++) 
for(int j = 0; j < population[i].size(); j++)

if(population[i][j] == 'i')

if(population[i-1][j] == 's')
population[i-1][j] = 'i';

if(population[i-1][j+1] == 's')
population[i-1][j+1] = 'i';

if(population[i][j+1] == 's')
population[i][j+1] = 'i';

if(population[i+1][j+1] == 's')
population[i+1][j+1] = 'i';

if(population[i+1][j] == 's')
population[i+1][j] = 'i';

if(population[i+1][j-1] == 's')
population[i+1][j-1] = 'i';

if(population[i][j-1] == 's')
population[i][j-1] = 'i';













share|improve this question



























    up vote
    0
    down vote

    favorite












    I have a vector of vectors filled with characters from a text file. It is essentially a simple outbreak simulator, with 'i' characters being infected, and 's' characters being susceptible to infection. The point is to run through the matrix and if it comes across an 'i', it then changes all 's' around it into an 'i'. I run into a problem when checking the elements around it due to checking positions out of the bounds on the edges of the matrix. Is there a way to check these bounds in my if statements?



    Here is the code:



    for (int i = 0; i < population.size(); i++) 
    for(int j = 0; j < population[i].size(); j++)

    if(population[i][j] == 'i')

    if(population[i-1][j] == 's')
    population[i-1][j] = 'i';

    if(population[i-1][j+1] == 's')
    population[i-1][j+1] = 'i';

    if(population[i][j+1] == 's')
    population[i][j+1] = 'i';

    if(population[i+1][j+1] == 's')
    population[i+1][j+1] = 'i';

    if(population[i+1][j] == 's')
    population[i+1][j] = 'i';

    if(population[i+1][j-1] == 's')
    population[i+1][j-1] = 'i';

    if(population[i][j-1] == 's')
    population[i][j-1] = 'i';













    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a vector of vectors filled with characters from a text file. It is essentially a simple outbreak simulator, with 'i' characters being infected, and 's' characters being susceptible to infection. The point is to run through the matrix and if it comes across an 'i', it then changes all 's' around it into an 'i'. I run into a problem when checking the elements around it due to checking positions out of the bounds on the edges of the matrix. Is there a way to check these bounds in my if statements?



      Here is the code:



      for (int i = 0; i < population.size(); i++) 
      for(int j = 0; j < population[i].size(); j++)

      if(population[i][j] == 'i')

      if(population[i-1][j] == 's')
      population[i-1][j] = 'i';

      if(population[i-1][j+1] == 's')
      population[i-1][j+1] = 'i';

      if(population[i][j+1] == 's')
      population[i][j+1] = 'i';

      if(population[i+1][j+1] == 's')
      population[i+1][j+1] = 'i';

      if(population[i+1][j] == 's')
      population[i+1][j] = 'i';

      if(population[i+1][j-1] == 's')
      population[i+1][j-1] = 'i';

      if(population[i][j-1] == 's')
      population[i][j-1] = 'i';













      share|improve this question















      I have a vector of vectors filled with characters from a text file. It is essentially a simple outbreak simulator, with 'i' characters being infected, and 's' characters being susceptible to infection. The point is to run through the matrix and if it comes across an 'i', it then changes all 's' around it into an 'i'. I run into a problem when checking the elements around it due to checking positions out of the bounds on the edges of the matrix. Is there a way to check these bounds in my if statements?



      Here is the code:



      for (int i = 0; i < population.size(); i++) 
      for(int j = 0; j < population[i].size(); j++)

      if(population[i][j] == 'i')

      if(population[i-1][j] == 's')
      population[i-1][j] = 'i';

      if(population[i-1][j+1] == 's')
      population[i-1][j+1] = 'i';

      if(population[i][j+1] == 's')
      population[i][j+1] = 'i';

      if(population[i+1][j+1] == 's')
      population[i+1][j+1] = 'i';

      if(population[i+1][j] == 's')
      population[i+1][j] = 'i';

      if(population[i+1][j-1] == 's')
      population[i+1][j-1] = 'i';

      if(population[i][j-1] == 's')
      population[i][j-1] = 'i';










      java matrix vector indexoutofboundsexception






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 3:58









      mjuarez

      9,36973651




      9,36973651










      asked Nov 10 at 3:42









      Bryan Murray

      1




      1






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          Instead of directly referencing a particular array entry, you could do something like the following:



          void checkForInfectionAndInfectIfNeeded(int i, int j) 
          for (int row = -1; row <= 1; row++)
          for (int column = -1; column <=1; column++)
          infect(i + row, j + column);




          void infect(int i, int j) j >= population[j].size())
          return;
          else
          population[i][j] = 'i';




          This way, the infect method is the only that checks the boundaries, and you replace your long list of manually checking the surrounding locations with two loops.






          share|improve this answer




















          • Awesome this is really helpful! I originally fixed it by adding those same checks in each if statement, but this works way easier!
            – Bryan Murray
            Nov 10 at 5:29










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235818%2fhow-do-i-check-if-a-position-in-my-vector-of-vectors-is-out-of-bounds%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          Instead of directly referencing a particular array entry, you could do something like the following:



          void checkForInfectionAndInfectIfNeeded(int i, int j) 
          for (int row = -1; row <= 1; row++)
          for (int column = -1; column <=1; column++)
          infect(i + row, j + column);




          void infect(int i, int j) j >= population[j].size())
          return;
          else
          population[i][j] = 'i';




          This way, the infect method is the only that checks the boundaries, and you replace your long list of manually checking the surrounding locations with two loops.






          share|improve this answer




















          • Awesome this is really helpful! I originally fixed it by adding those same checks in each if statement, but this works way easier!
            – Bryan Murray
            Nov 10 at 5:29














          up vote
          1
          down vote













          Instead of directly referencing a particular array entry, you could do something like the following:



          void checkForInfectionAndInfectIfNeeded(int i, int j) 
          for (int row = -1; row <= 1; row++)
          for (int column = -1; column <=1; column++)
          infect(i + row, j + column);




          void infect(int i, int j) j >= population[j].size())
          return;
          else
          population[i][j] = 'i';




          This way, the infect method is the only that checks the boundaries, and you replace your long list of manually checking the surrounding locations with two loops.






          share|improve this answer




















          • Awesome this is really helpful! I originally fixed it by adding those same checks in each if statement, but this works way easier!
            – Bryan Murray
            Nov 10 at 5:29












          up vote
          1
          down vote










          up vote
          1
          down vote









          Instead of directly referencing a particular array entry, you could do something like the following:



          void checkForInfectionAndInfectIfNeeded(int i, int j) 
          for (int row = -1; row <= 1; row++)
          for (int column = -1; column <=1; column++)
          infect(i + row, j + column);




          void infect(int i, int j) j >= population[j].size())
          return;
          else
          population[i][j] = 'i';




          This way, the infect method is the only that checks the boundaries, and you replace your long list of manually checking the surrounding locations with two loops.






          share|improve this answer












          Instead of directly referencing a particular array entry, you could do something like the following:



          void checkForInfectionAndInfectIfNeeded(int i, int j) 
          for (int row = -1; row <= 1; row++)
          for (int column = -1; column <=1; column++)
          infect(i + row, j + column);




          void infect(int i, int j) j >= population[j].size())
          return;
          else
          population[i][j] = 'i';




          This way, the infect method is the only that checks the boundaries, and you replace your long list of manually checking the surrounding locations with two loops.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 3:58









          mjuarez

          9,36973651




          9,36973651











          • Awesome this is really helpful! I originally fixed it by adding those same checks in each if statement, but this works way easier!
            – Bryan Murray
            Nov 10 at 5:29
















          • Awesome this is really helpful! I originally fixed it by adding those same checks in each if statement, but this works way easier!
            – Bryan Murray
            Nov 10 at 5:29















          Awesome this is really helpful! I originally fixed it by adding those same checks in each if statement, but this works way easier!
          – Bryan Murray
          Nov 10 at 5:29




          Awesome this is really helpful! I originally fixed it by adding those same checks in each if statement, but this works way easier!
          – Bryan Murray
          Nov 10 at 5:29

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235818%2fhow-do-i-check-if-a-position-in-my-vector-of-vectors-is-out-of-bounds%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Syphilis

          Darth Vader #20