<?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[Indexoperator für konstante Map]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe eine Klasse, welche als Member eine zwei maps enthält. Für std::string habe ich diese nun spezialisiert:<br />
[cpp]<br />
template&lt;&gt;<br />
class Measure<a href="std::string" rel="nofollow">std::string</a><br />
{<br />
typedef std::map&lt;Primitive,Scalar::BigInteger&gt;::const_iterator citer;<br />
protected:<br />
std::map&lt;Primitive,Scalar::BigInteger&gt; factors;<br />
std::map&lt;Primitive,std::string&gt; name;<br />
public:<br />
static std::map&lt;Primitive,std::string&gt; createNames //...</p>
<p>// Konstruktoren...<br />
// Operationen...</p>
<p>std::string toString() const<br />
{<br />
std::string value = &quot;[&quot;;<br />
for (citer i=factors.begin();i!=factors.end();i++)<br />
{<br />
if (i-&gt;second != 0)<br />
{<br />
value += &quot; &quot;;<br />
value += std::stream_cast<a href="std::string" rel="nofollow">std::string</a>(name[i-&gt;first]); // <strong>Problemstelle</strong><br />
if (i-&gt;second != 1)<br />
{<br />
value += &quot;^&quot;;<br />
value += std::stream_cast<a href="std::string" rel="nofollow">std::string</a>(i-&gt;second);<br />
}<br />
}<br />
}<br />
return (value += &quot; ]&quot;);<br />
}<br />
};[/cpp]</p>
<p>Zum Verständnis: Die Klasse stellt eine physikalische Einheit dar (z. B. Nm oder VA). Primitive ist ein enum, der alle SI-Einheiten enthält (METER, CANDELA etc.). Solche Einheiten bestehen immer aus einem Produkt von Potenzen von SI-Einheiten und einem Bruch und ggf. einer Zehnerpotenz. (Beispiel: N := 10^3 * g * m * s^-2) Um nun beim Ausgeben einen String zuordnen zu können, brauche ich natürlich noch eine map, die mir die Strings liefert.</p>
<p>Problemstelle: der Zugriff name[i-&gt;first]. Bei name handelt es sich ja (wegen const-Member) um eine konstante Map. Und map&lt;T1,T2&gt;::operator [](T1 index) scheint nicht const zu sein, so dass der Compiler nörgelt:</p>
<pre><code>&quot;measure.hpp&quot;: datatypes/measure.hpp passing `const std::map&lt;GeoMath::Measure::Primitive,std::string, std::less&lt;GeoMath::Measure::Primitive&gt;,
std::allocator&lt;std::pair&lt;const GeoMath::Measure::Primitive, std::string&gt; &gt; &gt;
' as `this' argument of `_Tp&amp; std::map&lt;_Key, _Tp, _Compare,
_Alloc&gt;::operator[](const _Key&amp;) [with _Key = GeoMath::Measure::Primitive,
_Tp = std::string, _Compare = std::less&lt;GeoMath::Measure::Primitive&gt;, _Alloc
= std::allocator&lt;std::pair&lt;const GeoMath::Measure::Primitive, std::string&gt;
&gt;]' discards qualifiers
</code></pre>
<p>Um das ganze mal zu verschlanken:</p>
<pre><code>&quot;measure.hpp&quot;: datatypes/measure.hpp passing `const std::map&lt;GeoMath::Measure::Primitive,std::string&gt;' as `this' argument of `_Tp&amp; std::map&lt;_Key, _Tp&gt;::operator[](const _Key&amp;)' discards qualifiers
</code></pre>
<p>Nun die Frage: wie kann ich (const) auf ein Map-Member zugreifen?</p>
<p>(PS: Warum wird in der Vorschau eigentlich das BB-Tag Code, aber nicht C++ formatiert?)</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/151592/indexoperator-für-konstante-map</link><generator>RSS for Node</generator><lastBuildDate>Sun, 06 Sep 2026 16:00:09 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/151592.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 27 Jun 2006 19:15:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Indexoperator für konstante Map on Tue, 27 Jun 2006 19:15:26 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe eine Klasse, welche als Member eine zwei maps enthält. Für std::string habe ich diese nun spezialisiert:<br />
[cpp]<br />
template&lt;&gt;<br />
class Measure<a href="std::string" rel="nofollow">std::string</a><br />
{<br />
typedef std::map&lt;Primitive,Scalar::BigInteger&gt;::const_iterator citer;<br />
protected:<br />
std::map&lt;Primitive,Scalar::BigInteger&gt; factors;<br />
std::map&lt;Primitive,std::string&gt; name;<br />
public:<br />
static std::map&lt;Primitive,std::string&gt; createNames //...</p>
<p>// Konstruktoren...<br />
// Operationen...</p>
<p>std::string toString() const<br />
{<br />
std::string value = &quot;[&quot;;<br />
for (citer i=factors.begin();i!=factors.end();i++)<br />
{<br />
if (i-&gt;second != 0)<br />
{<br />
value += &quot; &quot;;<br />
value += std::stream_cast<a href="std::string" rel="nofollow">std::string</a>(name[i-&gt;first]); // <strong>Problemstelle</strong><br />
if (i-&gt;second != 1)<br />
{<br />
value += &quot;^&quot;;<br />
value += std::stream_cast<a href="std::string" rel="nofollow">std::string</a>(i-&gt;second);<br />
}<br />
}<br />
}<br />
return (value += &quot; ]&quot;);<br />
}<br />
};[/cpp]</p>
<p>Zum Verständnis: Die Klasse stellt eine physikalische Einheit dar (z. B. Nm oder VA). Primitive ist ein enum, der alle SI-Einheiten enthält (METER, CANDELA etc.). Solche Einheiten bestehen immer aus einem Produkt von Potenzen von SI-Einheiten und einem Bruch und ggf. einer Zehnerpotenz. (Beispiel: N := 10^3 * g * m * s^-2) Um nun beim Ausgeben einen String zuordnen zu können, brauche ich natürlich noch eine map, die mir die Strings liefert.</p>
<p>Problemstelle: der Zugriff name[i-&gt;first]. Bei name handelt es sich ja (wegen const-Member) um eine konstante Map. Und map&lt;T1,T2&gt;::operator [](T1 index) scheint nicht const zu sein, so dass der Compiler nörgelt:</p>
<pre><code>&quot;measure.hpp&quot;: datatypes/measure.hpp passing `const std::map&lt;GeoMath::Measure::Primitive,std::string, std::less&lt;GeoMath::Measure::Primitive&gt;,
std::allocator&lt;std::pair&lt;const GeoMath::Measure::Primitive, std::string&gt; &gt; &gt;
' as `this' argument of `_Tp&amp; std::map&lt;_Key, _Tp, _Compare,
_Alloc&gt;::operator[](const _Key&amp;) [with _Key = GeoMath::Measure::Primitive,
_Tp = std::string, _Compare = std::less&lt;GeoMath::Measure::Primitive&gt;, _Alloc
= std::allocator&lt;std::pair&lt;const GeoMath::Measure::Primitive, std::string&gt;
&gt;]' discards qualifiers
</code></pre>
<p>Um das ganze mal zu verschlanken:</p>
<pre><code>&quot;measure.hpp&quot;: datatypes/measure.hpp passing `const std::map&lt;GeoMath::Measure::Primitive,std::string&gt;' as `this' argument of `_Tp&amp; std::map&lt;_Key, _Tp&gt;::operator[](const _Key&amp;)' discards qualifiers
</code></pre>
<p>Nun die Frage: wie kann ich (const) auf ein Map-Member zugreifen?</p>
<p>(PS: Warum wird in der Vorschau eigentlich das BB-Tag Code, aber nicht C++ formatiert?)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1087099</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1087099</guid><dc:creator><![CDATA[zral]]></dc:creator><pubDate>Tue, 27 Jun 2006 19:15:26 GMT</pubDate></item><item><title><![CDATA[Reply to Indexoperator für konstante Map on Tue, 27 Jun 2006 19:38:29 GMT]]></title><description><![CDATA[<p>zral schrieb:</p>
<blockquote>
<p>Problemstelle: der Zugriff name[i-&gt;first]. Bei name handelt es sich ja (wegen const-Member) um eine konstante Map. Und map&lt;T1,T2&gt;::operator [](T1 index) scheint nicht const zu sein, so dass der Compiler nörgelt:</p>
</blockquote>
<p>richtig, denn der operator verändert die map ja potentiell: wenn ein element noch nicht existiert, wird es eingefügt (mit default-konstruiertem value)</p>
<blockquote>
<p>Nun die Frage: wie kann ich (const) auf ein Map-Member zugreifen?</p>
</blockquote>
<p>benutz find. sinnvoller wäre hier aber wohl, nur eine map zu benutzen z.b.</p>
<pre><code class="language-cpp">std::map&lt;Primitive,std::pair&lt;Scalar::BigInteger,std::string&gt; &gt;
</code></pre>
<p>evtl. auch interessant: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#464" rel="nofollow">Suggestion for new member functions in standard containers</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1087120</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1087120</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Tue, 27 Jun 2006 19:38:29 GMT</pubDate></item><item><title><![CDATA[Reply to Indexoperator für konstante Map on Wed, 28 Jun 2006 10:53:27 GMT]]></title><description><![CDATA[<p>Dann kann ich ja aber eigentlich auch gleich einen vector nehmen. Es sind ja eigentlich immer max. soviele Elemente, wie es SI-Einheiten gibt. Und einen Vektor kann ich schließlich auch mit einem enum indizieren (vorher natürlich in einen int casten) - so müsste das funktionieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1087501</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1087501</guid><dc:creator><![CDATA[zral]]></dc:creator><pubDate>Wed, 28 Jun 2006 10:53:27 GMT</pubDate></item></channel></rss>