title: Why does std::map operator[] create an object if the key doesn't exist?
body: <p>I'm pretty sure I already saw this question somewhere (comp.lang.c++? Google doesn't seem to find it there either), but a quick search here doesn't seem to find it, so here it is:</p>
  <p>Why does <code>std::map operator[]</code> create an object if the key doesn't exist? I don't know but for me this seems counter-intuitive if you compare to most other operator[] (like std::vector) where if you use it you must be sure that the index exists. I'm wondering what's the rationale for implementing this behavior in <code>std::map</code>. Like I said wouldn't it be more intuitive to act more like an index in a vector and crash (well undefined behavior I guess) when accessed with an invalid key?</p>
  <p>I've gotten a lot of answers saying basically &quot;it's cheap, so why not?&quot; or similar. I totally agree with that, but why not use a dedicated function for that (I think one of the comments said that in Java there is no <code>operator[]</code> and the function is called <code>put</code>)? My point is why doesn't <code>map operator[]</code> work like a vector? If I use <code>operator[]</code> on an out of range index on a vector I wouldn't like it to insert an element <strong>even if it was cheap</strong> because that probably mean an error in my code. My point is why isn't it the same thing with map. I mean, for me, using <code>operator[]</code> on a map would mean: i know this key already exist (for whatever reason, I just inserted it, I have redundancy somewhere, whatever). I think it would be more intuitive that way.</p>
  <p>That said what are the advantage of doing the current behavior with operator[] (and only for that, I agree that a function with the current behavior should be there, just not <code>operator[])</code>? Maybe it give clearer code that way? I don't know.</p>
  <p>Another answer was that it already existed that way so why not keep it but then, probably when they (the ones before stl) choose to implement it that way they found it provided an advantage or something? So my question is basically: why choose to implement it that way, meaning a somewhat lack of consistency with other <code>operator[]</code>. What benefit do it give?</p>