title: C++ Vector subscript out of range when assigning values
body: <p>I am making a simple algorithm that counts how many times each number is represented in a vector. However, on compile it gives me the following error in popup:  <strong>Vector subscript out of range</strong> and it is referencing to:</p>
  <blockquote>
  <p>File: C:\Program Files\Microsoft Visual
  Studio\2022\Community\VC\Tools\MSVC\14.31.31103\include\vector</p>
  </blockquote>
  <blockquote>
  <p>Line:
  1463</p>
  </blockquote>
  <p>Code:</p>
  <pre><code>//get if highest value is negative
  if (mx &lt; 0) {
  //store number of invidiual values in array invert, mn value
      std::vector&lt;int&gt; vl(mn * -1);
      for (int x = 0; x &lt; arr.size(); x++) {
          vl[arr[x] * -1] += 1;
      }
  </code></pre>
  <p>Here I am working with negative values so I am inverting them to positive. Mn is representing the lowest value in the vector, which is then inverted so it becomes the highest, according to is created another vector Vl which stores a number of each value, so its size is mn * -1.</p>
  <p>Full function:</p>
  <pre><code>void topSort(std::vector&lt;int&gt; &amp;arr) {
  
  //stores highest value
  int mx = arr[0];
  //stores lowest value
  int mn = arr[0];
  
  //get highest value
  for (int i = 1; i &lt; arr.size(); i++) {
      if (arr[i] &gt; mx) {
          mx = arr[i];
      }
      if (arr[i] &lt; mn) {
          mn = arr[i];
      }
  }
  //get if highest value is negative
  if (mx &lt; 0) {
      //store number of invidiual values in array invert mx value
      std::vector&lt;int&gt; vl(mn * -1);
      for (int x = 0; x &lt; arr.size(); x++) {
          vl[arr[x] * -1] += 1;
      }
      for (auto z : vl) {
          std::cout &lt;&lt; z &lt;&lt; &quot;\n&quot;;
      }
  } else {
      //store number of invidiual values in array
      std::vector&lt;int&gt; vl(mx);
  }
  }
  </code></pre>