title: Why C++ STL vector does not go out of range
body: <p>I have this code and I am wondering how it works; Why does it allow me to access an element using a value larger than the size of the vector using the <code>operator[]</code>? </p>
  
  <p>But then when I use the <code>at()</code> function which does bounds checking, it throws the proper error.</p>
  
  <p>I read that the behavior of doing this is <code>undefined</code>, but I am curious: Why does <code>operator[]</code> work for out of range element access? </p>
  
  <pre><code>// vector of length 3
  std::vector&lt;int&gt; adj(3);
  
  // output: 3
  printf("Size of adj is %lu\n", adj.size());
  
  // assign using index that is larger than vector size, e.g., 12
  adj[12] = 314159;
  
  // succeeds, output: 314159
  printf("adj[12] is %d", adj[12]);
  
  // fails, throws out_of_range
  adj.at(12);
  </code></pre>