title: Const map element access
body: <p>I tried to use the <code>operator[]</code> access the element in a <code>const map</code>, but this method failed. I also tried to use <code>at()</code> to do the same thing. It worked this time. However, I could not find any reference about using <code>at()</code> to access element in a <code>const map</code>. Is <code>at()</code> a newly added function in <code>map</code>? Where can I find more info about this? Thank you very much!</p>
  <p>An example could be the following:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;map&gt;
  
  using namespace std;
  
  int main()
  {
          map&lt;int, char&gt; A;
          A[1] = 'b';
          A[3] = 'c';
  
          const map&lt;int, char&gt; B = A;
  
          cout &lt;&lt; B.at(3) &lt;&lt; endl; // it works
          cout &lt;&lt; B[3] &lt;&lt; endl;  // it does not work
  
  }
  </code></pre>
  <p>For using &quot;B[3]&quot;, it returned the following errors during compiling:</p>
  <blockquote>
  <p>t01.cpp:14: error: passing ‘const
  std::map&lt;int, char, std::less,
  std::allocator&lt;std::pair&lt;const int,
  char&gt; &gt; &gt;’ as ‘this’ argument of ‘_Tp&amp;
  std::map&lt;_Key, _Tp, _Compare,
  _Alloc&gt;::operator[](const _Key&amp;) [with _Key = int, _Tp = char, _Compare = std::less, _Alloc =
  std::allocator&lt;std::pair&lt;const int,
  char&gt; &gt;]’ discards qualifiers</p>
  </blockquote>
  <p>The compiler used is g++ 4.2.1</p>