<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[const std::map&amp;lt;T,T&amp;gt; und der operator [] ?]]></title><description><![CDATA[<p>Warum funktioniert der operator[] nicht auf const maps?<br />
Auf cppref finde ich keine gescheite Antwort dazu und nach const map zu suchen ist nicht so erfolgreich</p>
<pre><code class="language-cpp">#include &lt;map&gt;
int main()
{
std::map&lt;double,int&gt; g;
g.insert(std::pair&lt;double,int&gt;(1,2));
const std::map&lt;double,int&gt; cg = g;
int gf = cg[1];
}
</code></pre>
<blockquote>
<p>Eerror C2678: binary '[' : no operator found which takes a left-hand operand of type 'const std::map&lt;_Kty,_Ty&gt;' (or there is no acceptable conversion)</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/topic/235086/const-std-map-lt-t-t-gt-und-der-operator</link><generator>RSS for Node</generator><lastBuildDate>Thu, 24 Sep 2026 07:24:02 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/235086.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 25 Feb 2009 13:57:13 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to const std::map&amp;lt;T,T&amp;gt; und der operator [] ? on Wed, 25 Feb 2009 13:57:13 GMT]]></title><description><![CDATA[<p>Warum funktioniert der operator[] nicht auf const maps?<br />
Auf cppref finde ich keine gescheite Antwort dazu und nach const map zu suchen ist nicht so erfolgreich</p>
<pre><code class="language-cpp">#include &lt;map&gt;
int main()
{
std::map&lt;double,int&gt; g;
g.insert(std::pair&lt;double,int&gt;(1,2));
const std::map&lt;double,int&gt; cg = g;
int gf = cg[1];
}
</code></pre>
<blockquote>
<p>Eerror C2678: binary '[' : no operator found which takes a left-hand operand of type 'const std::map&lt;_Kty,_Ty&gt;' (or there is no acceptable conversion)</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/1670003</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1670003</guid><dc:creator><![CDATA[Seikilos]]></dc:creator><pubDate>Wed, 25 Feb 2009 13:57:13 GMT</pubDate></item><item><title><![CDATA[Reply to const std::map&amp;lt;T,T&amp;gt; und der operator [] ? on Wed, 25 Feb 2009 13:59:12 GMT]]></title><description><![CDATA[<p>Ah, er ist nicht const, weil er neue Elemente einfügen kann, schon klar.</p>
<p>Erledigt, danke <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1670007</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1670007</guid><dc:creator><![CDATA[Seikilos]]></dc:creator><pubDate>Wed, 25 Feb 2009 13:59:12 GMT</pubDate></item><item><title><![CDATA[Reply to const std::map&amp;lt;T,T&amp;gt; und der operator [] ? on Wed, 25 Feb 2009 14:00:16 GMT]]></title><description><![CDATA[<p>Seikilos schrieb:</p>
<blockquote>
<p>Warum funktioniert der operator[] nicht auf const maps?<br />
Auf cppref finde ich keine gescheite Antwort dazu und nach const map zu suchen ist nicht so erfolgreich</p>
<pre><code class="language-cpp">#include &lt;map&gt;
int main()
{
std::map&lt;double,int&gt; g;
g.insert(std::pair&lt;double,int&gt;(1,2));
const std::map&lt;double,int&gt; cg = g;
int gf = cg[1];
}
</code></pre>
<blockquote>
<p>Eerror C2678: binary '[' : no operator found which takes a left-hand operand of type 'const std::map&lt;_Kty,_Ty&gt;' (or there is no acceptable conversion)</p>
</blockquote>
</blockquote>
<p>Weil Du über den operator den Zustand der Map ändern kannst. Er ist keine const-Methode.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1670008</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1670008</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Wed, 25 Feb 2009 14:00:16 GMT</pubDate></item><item><title><![CDATA[Reply to const std::map&amp;lt;T,T&amp;gt; und der operator [] ? on Wed, 25 Feb 2009 15:39:18 GMT]]></title><description><![CDATA[<p>Tachyon schrieb:</p>
<blockquote>
<p>Weil Du über den operator den Zustand der Map ändern kannst. Er ist keine const-Methode.</p>
</blockquote>
<p>Muesste es dafuer dann nicht eine ueberladene operator[]-Funktion geben, die dann statt einer Referenz eine konstante Referenz zurueckgibt? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
<p>Gruss<br />
Cartman</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1670111</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1670111</guid><dc:creator><![CDATA[Eric Cartman]]></dc:creator><pubDate>Wed, 25 Feb 2009 15:39:18 GMT</pubDate></item><item><title><![CDATA[Reply to const std::map&amp;lt;T,T&amp;gt; und der operator [] ? on Wed, 25 Feb 2009 15:42:19 GMT]]></title><description><![CDATA[<p>Laut Ref nicht, wäre aber schön <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1670116</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1670116</guid><dc:creator><![CDATA[Seikilos]]></dc:creator><pubDate>Wed, 25 Feb 2009 15:42:19 GMT</pubDate></item><item><title><![CDATA[Reply to const std::map&amp;lt;T,T&amp;gt; und der operator [] ? on Wed, 25 Feb 2009 15:42:52 GMT]]></title><description><![CDATA[<p>Eric Cartman schrieb:</p>
<blockquote>
<p>Tachyon schrieb:</p>
<blockquote>
<p>Weil Du über den operator den Zustand der Map ändern kannst. Er ist keine const-Methode.</p>
</blockquote>
<p>Muesste es dafuer dann nicht eine ueberladene operator[]-Funktion geben, die dann statt einer Referenz eine konstante Referenz zurueckgibt? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /><br />
Gruss<br />
Cartman</p>
</blockquote>
<p>Die gibts deshalb nicht, weil map&lt;&gt;::operator[] bei Bedarf ein zusätzliches Key-Value-Paar in der Map anlegt, nämlich dann wenn der geforderte Key nicht vorhanden ist. Diese Neuanlage verändert die Map -&gt; nicht const.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1670119</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1670119</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Wed, 25 Feb 2009 15:42:52 GMT</pubDate></item><item><title><![CDATA[Reply to const std::map&amp;lt;T,T&amp;gt; und der operator [] ? on Wed, 25 Feb 2009 15:54:17 GMT]]></title><description><![CDATA[<p>Ja und ne überladende könnte ja nur holen, ohne anlegen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1670123</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1670123</guid><dc:creator><![CDATA[Seikilos]]></dc:creator><pubDate>Wed, 25 Feb 2009 15:54:17 GMT</pubDate></item><item><title><![CDATA[Reply to const std::map&amp;lt;T,T&amp;gt; und der operator [] ? on Wed, 25 Feb 2009 15:54:42 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>Eric Cartman schrieb:</p>
<blockquote>
<p>Tachyon schrieb:</p>
<blockquote>
<p>Weil Du über den operator den Zustand der Map ändern kannst. Er ist keine const-Methode.</p>
</blockquote>
<p>Muesste es dafuer dann nicht eine ueberladene operator[]-Funktion geben, die dann statt einer Referenz eine konstante Referenz zurueckgibt? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /><br />
Gruss<br />
Cartman</p>
</blockquote>
<p>Die gibts deshalb nicht, weil map&lt;&gt;::operator[] bei Bedarf ein zusätzliches Key-Value-Paar in der Map anlegt, nämlich dann wenn der geforderte Key nicht vorhanden ist. Diese Neuanlage verändert die Map -&gt; nicht const.</p>
</blockquote>
<p>Klingt einleuchtend. Danke fuer die Aufklaerung.<br />
(muss zugeben, ich habe bis jetzt noch nicht mit der map gearbeitet)</p>
<p>Gruss<br />
Cartman</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1670125</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1670125</guid><dc:creator><![CDATA[Eric Cartman]]></dc:creator><pubDate>Wed, 25 Feb 2009 15:54:42 GMT</pubDate></item><item><title><![CDATA[Reply to const std::map&amp;lt;T,T&amp;gt; und der operator [] ? on Wed, 25 Feb 2009 15:55:36 GMT]]></title><description><![CDATA[<p>Seikilos schrieb:</p>
<blockquote>
<p>Ja und ne überladende könnte ja nur holen, ohne anlegen</p>
</blockquote>
<p>Genau das dachte ich mir eben auch <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>Gruss<br />
Cartman</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1670128</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1670128</guid><dc:creator><![CDATA[Eric Cartman]]></dc:creator><pubDate>Wed, 25 Feb 2009 15:55:36 GMT</pubDate></item><item><title><![CDATA[Reply to const std::map&amp;lt;T,T&amp;gt; und der operator [] ? on Wed, 25 Feb 2009 15:57:15 GMT]]></title><description><![CDATA[<p>meistens will man map::find() statt map::operator[]</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1670130</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1670130</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Wed, 25 Feb 2009 15:57:15 GMT</pubDate></item><item><title><![CDATA[Reply to const std::map&amp;lt;T,T&amp;gt; und der operator [] ? on Wed, 25 Feb 2009 16:57:57 GMT]]></title><description><![CDATA[<p>Seikilos schrieb:</p>
<blockquote>
<p>Ja und ne überladende könnte ja nur holen, ohne anlegen</p>
</blockquote>
<p>und was soll der dann zurückgeben wenns den Key nicht gibt? Da bleibt nicht viel außer ner exception - und das Verhalten ist schnell selbst implementiert:</p>
<pre><code class="language-cpp">template &lt;class Key, class T, class Comp, class Alloc&gt;
std::map&lt;Key,T,Comp,Alloc&gt;::const_reference find_or_throw(std::map&lt;Key,T,Comp,Alloc&gt; const&amp; m, Key const&amp; k)
{
   std::map&lt;Key,T,Comp,Alloc&gt;::const_iterator pos = m.find(k);
   if (pos == m.end()) throw DamnMyMapHasNotThatKeyException();
   else return *pos;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1670191</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1670191</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Wed, 25 Feb 2009 16:57:57 GMT</pubDate></item></channel></rss>