text: Q:
  
  Codeblocks c++ code doesn't run in VS 19 (vector subscript out of range)
  
  I ran a few tests of a simple problem in both VS 19 and CodeBlocks after an error that keeps popping in a lot of my coding problems. Surprisingly in Codeblocks everything runs fine and i get the expected result while in VS i always get  the "vector subscript out of range" error.
  The problem is quite simple, get the least number of stops until the destination. (just count them)
  The error appears at the second while loop where i compare i to the size of the vector.
  I am wondering why it actually works in codeblocks and also would like to know how to fix it in VS
  
  #include <iostream>
  #include <vector>
  
  using namespace std;
  
  int compute_min_refills(int dist, int tank, vector<int>& stations) {
  
      int distanceTraversed = 0;
      int i = 0;
      int stops = 0;
      bool impossible = false;    
      while (distanceTraversed<=dist)
      {
          if((distanceTraversed + tank)> dist)
          {
              distanceTraversed = dist;
              return stops;
          }
          while ((distanceTraversed + tank) > stations[i] && i< stations.size())
          {
              i++;
              impossible = false;
          }
  
          if ((distanceTraversed + tank) <= stations[i] || i == stations.size())
          {
              if (impossible == true)
              {
                  return -1;
              }
  
              else
              {
              distanceTraversed += stations[i - 1];
              stops++;
              impossible = true;
              }
  
          }
      }
  
      return stops;
  }
  
  int main() {
      int d = 0;
      cin >> d;
      int m = 0;
      cin >> m;
      int n = 0;
      cin >> n;
  
      vector<int> stations;
      int a;
      for (int i = 0; i < n; ++i)
      {
          cin>>a;
          stations.push_back(a);
      }
      cout << compute_min_refills(d, m, stations) << "\n";
  
      return 0;
  }
  
  Here are some imputs if you want to test this out:
  Input:
  500
  200
  4
  100 200 300 400
  
  A:
  
  You're bound checking your vector accesses too late.
  Because of  short-circuit evaluation, (distanceTraversed + tank) > stations[i] && i< stations.size() doesn't work the way you think it does, 1st you access the array, and only if the condition was true, you check if the access was inside bounds, which is UB if it was not.
  Later on (distanceTraversed + tank) <= stations[i] || i == stations.size() doesn't check for bounds either.
  And as a final note, distanceTraversed += stations[i - 1]; if i==0, you will try to access element -1, but std::vector is indexed using std::size_t which is unsigned, causing an implicit conversion, an assigning very large value to it. 
  
  