<?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[ids einer map schnell ändern]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>ich habe eine map mit bestehenden ids {1,2,..,3,n}. Jetzt möchte ich die ids möglichst schnell und intelligent mit einem offset z.B. {1001,1002,...,100+n} versehen.</p>
<p>Gibt es dafür eine intelligente Lösung (ohne schleifen).</p>
<p>Danke</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/290255/ids-einer-map-schnell-ändern</link><generator>RSS for Node</generator><lastBuildDate>Tue, 18 Aug 2026 16:07:56 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/290255.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 22 Jul 2011 11:15:37 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to ids einer map schnell ändern on Fri, 22 Jul 2011 11:15:37 GMT]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>ich habe eine map mit bestehenden ids {1,2,..,3,n}. Jetzt möchte ich die ids möglichst schnell und intelligent mit einem offset z.B. {1001,1002,...,100+n} versehen.</p>
<p>Gibt es dafür eine intelligente Lösung (ohne schleifen).</p>
<p>Danke</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2096446</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2096446</guid><dc:creator><![CDATA[asdsad]]></dc:creator><pubDate>Fri, 22 Jul 2011 11:15:37 GMT</pubDate></item><item><title><![CDATA[Reply to ids einer map schnell ändern on Fri, 22 Jul 2011 11:19:12 GMT]]></title><description><![CDATA[<p>Einen Wrapper um die map machen, der beim Durchschleifen den Offset addiert bzw. subtrahiert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2096449</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2096449</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 22 Jul 2011 11:19:12 GMT</pubDate></item><item><title><![CDATA[Reply to ids einer map schnell ändern on Fri, 22 Jul 2011 11:27:27 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>Einen Wrapper um die map machen, der beim Durchschleifen den Offset addiert bzw. subtrahiert.</p>
</blockquote>
<p>vll. gibts da was wie std::offset(int) ?!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2096453</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2096453</guid><dc:creator><![CDATA[asdsad]]></dc:creator><pubDate>Fri, 22 Jul 2011 11:27:27 GMT</pubDate></item><item><title><![CDATA[Reply to ids einer map schnell ändern on Fri, 22 Jul 2011 11:31:34 GMT]]></title><description><![CDATA[<p>asdsad schrieb:</p>
<blockquote>
<p>volkard schrieb:</p>
<blockquote>
<p>Einen Wrapper um die map machen, der beim Durchschleifen den Offset addiert bzw. subtrahiert.</p>
</blockquote>
<p>vll. gibts da was wie std::offset(int) ?!</p>
</blockquote>
<p>Nein was gemeint ist, ist folgendes:</p>
<pre><code class="language-cpp">class MyMap {
private:
   std::map&lt;int, Foo&gt; map;
   int offset;

public:
   void setOffset(int offset) {
      this-&gt;offset = offset;
   }
   Foo&amp; operator[](int id) {
      return map[id+offset];
   }
};
</code></pre>
<p>bzw. statt offset kann ja auch eine Funktion verwendet werden die die ID korrekt transformiert.</p>
<p>Sowas ist deutlich effizienter als die map jedesmal neu aufzubauen mit anderen keys.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2096454</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2096454</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Fri, 22 Jul 2011 11:31:34 GMT</pubDate></item><item><title><![CDATA[Reply to ids einer map schnell ändern on Fri, 22 Jul 2011 11:35:08 GMT]]></title><description><![CDATA[<p>Shade Of Mine schrieb:</p>
<blockquote>
<p>asdsad schrieb:</p>
<blockquote>
<p>volkard schrieb:</p>
<blockquote>
<p>Einen Wrapper um die map machen, der beim Durchschleifen den Offset addiert bzw. subtrahiert.</p>
</blockquote>
<p>vll. gibts da was wie std::offset(int) ?!</p>
</blockquote>
<p>Nein was gemeint ist, ist folgendes:</p>
<pre><code class="language-cpp">class MyMap {
private:
   std::map&lt;int, Foo&gt; map;
   int offset;

public:
   void setOffset(int offset) {
      this-&gt;offset = offset;
   }
   Foo&amp; operator[](int id) {
      return map[id+offset];
   }
};
</code></pre>
<p>bzw. statt offset kann ja auch eine Funktion verwendet werden die die ID korrekt transformiert.</p>
<p>Sowas ist deutlich effizienter als die map jedesmal neu aufzubauen mit anderen keys.</p>
</blockquote>
<p>Kann leider an der schnittstelle nichts ändern, sie verlangt eine map... ich überlege einen cast operator für die o.g. MyMap zu schreiben aber das wäre dann etwas dirty..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2096459</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2096459</guid><dc:creator><![CDATA[asdsad]]></dc:creator><pubDate>Fri, 22 Jul 2011 11:35:08 GMT</pubDate></item><item><title><![CDATA[Reply to ids einer map schnell ändern on Fri, 22 Jul 2011 11:36:30 GMT]]></title><description><![CDATA[<p>asdsad schrieb:</p>
<blockquote>
<p>vll. gibts da was wie std::offset(int) ?!</p>
</blockquote>
<p><a href="http://www.cplusplus.com/reference/clibrary/cstddef/offsetof/" rel="nofollow">http://www.cplusplus.com/reference/clibrary/cstddef/offsetof/</a><br />
Aber das ist nur eine Namensähnlichkeit und hier nicht zielführend. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2096461</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2096461</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 22 Jul 2011 11:36:30 GMT</pubDate></item><item><title><![CDATA[Reply to ids einer map schnell ändern on Fri, 22 Jul 2011 11:37:55 GMT]]></title><description><![CDATA[<p>asdsad schrieb:</p>
<blockquote>
<p>Kann leider an der schnittstelle nichts ändern, sie verlangt eine map... ich überlege einen cast operator für die o.g. MyMap zu schreiben aber das wäre dann etwas dirty..</p>
</blockquote>
<p>dh du kannst auch nicht die Klasse ändern die als Key verwendet wird? Sonst könnte man dort nämlich am operator&lt; ansetzen.</p>
<p>Dann fällt mir aber nichts mehr ein und man muss echt dumm kopieren.<br />
Was kannst du denn ändern? Vielleicht fällt uns so eine Lösung ein...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2096463</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2096463</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Fri, 22 Jul 2011 11:37:55 GMT</pubDate></item><item><title><![CDATA[Reply to ids einer map schnell ändern on Fri, 22 Jul 2011 11:38:33 GMT]]></title><description><![CDATA[<p>asdsad schrieb:</p>
<blockquote>
<p>Kann leider an der schnittstelle nichts ändern, sie verlangt eine map... ich überlege einen cast operator für die o.g. MyMap zu schreiben aber das wäre dann etwas dirty..</p>
</blockquote>
<p>Verlangt sie auch eine map&lt;int&gt; oder darfs eine map&lt;offsettable_int&lt;&amp;global_offset&gt;&gt; sein?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2096464</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2096464</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 22 Jul 2011 11:38:33 GMT</pubDate></item><item><title><![CDATA[Reply to ids einer map schnell ändern on Fri, 22 Jul 2011 11:38:58 GMT]]></title><description><![CDATA[<p>Das Problem ist, dass du die Keys einer Map nicht ändern darfst, selbst wenn die Sortierung anschliessend erhalten bleibt. Wenn du also nach wie vor eine <code>std::map&lt;int, X&gt;</code> benötigst, gibt es keinen anderen <em>standardkonformen</em> Weg, als eine neue Map mit verschobenen Indizes zu erstellen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2096465</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2096465</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Fri, 22 Jul 2011 11:38:58 GMT</pubDate></item><item><title><![CDATA[Reply to ids einer map schnell ändern on Fri, 22 Jul 2011 11:42:59 GMT]]></title><description><![CDATA[<p>Danke schonmal an alle Antwortenden</p>
<p>Shade Of Mine schrieb:</p>
<blockquote>
<p>asdsad schrieb:</p>
<blockquote>
<p>Kann leider an der schnittstelle nichts ändern, sie verlangt eine map... ich überlege einen cast operator für die o.g. MyMap zu schreiben aber das wäre dann etwas dirty..</p>
</blockquote>
<p>dh du kannst auch nicht die Klasse ändern die als Key verwendet wird? Sonst könnte man dort nämlich am operator&lt; ansetzen.</p>
<p>Dann fällt mir aber nichts mehr ein und man muss echt dumm kopieren.<br />
Was kannst du denn ändern? Vielleicht fällt uns so eine Lösung ein...</p>
</blockquote>
<p>Leider nicht... es wird eine std::map&lt;int,Target&gt; erzwungen die beginnend von 1 gefüllt wird während eine andere Klasse die ich ebenfalls nicht ändern kann für bestimmte Aufgaben ids beginnend ab 10000 benötigt weil sie die Zuordnung anhand von eben diesen Offsets vornimmt. Ich dachte da gäbe es etwas konventionelles einzeiliges...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2096468</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2096468</guid><dc:creator><![CDATA[asdsad]]></dc:creator><pubDate>Fri, 22 Jul 2011 11:42:59 GMT</pubDate></item><item><title><![CDATA[Reply to ids einer map schnell ändern on Fri, 22 Jul 2011 11:48:11 GMT]]></title><description><![CDATA[<p>Es gibt schon Möglichkeiten, aber das Verhalten ist laut C++-Standard undefiniert, weil Keys in einer Map nicht geändert werden dürfen.</p>
<pre><code class="language-cpp">std::map&lt;int, X&gt; m;
std::for_each(m.begin(), m.end(), [] (std::pair&lt;const int, X&gt;&amp; p) { const_cast&lt;int&amp;&gt;(p.first) += 1000; } );
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2096469</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2096469</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Fri, 22 Jul 2011 11:48:11 GMT</pubDate></item><item><title><![CDATA[Reply to ids einer map schnell ändern on Fri, 22 Jul 2011 11:49:11 GMT]]></title><description><![CDATA[<p>Angst hab, weil früher oder später irgendwann jemand die Iteratoren so bastelt, daß sie von der Ordnung abhängen, fürchte ich.<br />
Aber bei for_each(m.regin(), m.rend() hätte ich keine Angst mehr.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2096470</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2096470</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 22 Jul 2011 11:49:11 GMT</pubDate></item><item><title><![CDATA[Reply to ids einer map schnell ändern on Fri, 22 Jul 2011 11:50:46 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>Es gibt schon Möglichkeiten, aber das Verhalten ist laut C++-Standard undefiniert, weil Keys in einer Map nicht geändert werden dürfen.</p>
<pre><code class="language-cpp">std::map&lt;int, X&gt; m;
std::for_each(m.begin(), m.end(), [] (std::pair&lt;const int, X&gt;&amp; p) { const_cast&lt;int&amp;&gt;(p.first) += 1000; } );
</code></pre>
</blockquote>
<p>Die Reihenfolge ändert sich ja (in diesem Fall) nicht. Aber danke für den Tipp..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2096471</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2096471</guid><dc:creator><![CDATA[asdsad]]></dc:creator><pubDate>Fri, 22 Jul 2011 11:50:46 GMT</pubDate></item><item><title><![CDATA[Reply to ids einer map schnell ändern on Fri, 22 Jul 2011 11:55:11 GMT]]></title><description><![CDATA[<p>asdsad schrieb:</p>
<blockquote>
<p>Die Reihenfolge ändert sich ja (in diesem Fall) nicht.</p>
</blockquote>
<p>Trotzdem ist es undefiniertes Verhalten. Eine Map-Implementierung darf davon ausgehen, dass die Keys sich nie ändern, und darauf basierend z.B. irgendwelche Optimierungen anstellen. Wenn du als Benutzer diese Garantie brichst (mit <code>const_cast</code> als Brecheisen), musst du auch Manns genug sein, die Konsequenzen zu tragen.</p>
<p>Von daher: Kopier die Map lieber.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2096473</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2096473</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Fri, 22 Jul 2011 11:55:11 GMT</pubDate></item><item><title><![CDATA[Reply to ids einer map schnell ändern on Fri, 22 Jul 2011 11:56:41 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>Von daher: Kopier die Map lieber.</p>
</blockquote>
<p>Ich glaube da bleibt nichts anderes übrigt.</p>
<p>Danke an alle nochmal.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2096475</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2096475</guid><dc:creator><![CDATA[asdsad]]></dc:creator><pubDate>Fri, 22 Jul 2011 11:56:41 GMT</pubDate></item><item><title><![CDATA[Reply to ids einer map schnell ändern on Fri, 22 Jul 2011 12:00:56 GMT]]></title><description><![CDATA[<p>Ist der entsprechende Abschnitt in deinem Code wirklich derart performancekritisch?</p>
<p>Falls du die Geschwindigkeit nicht unbedingt brauchst, würde ich nicht lange überlegen, um zwischen einer standardkonformen und einer schnellen Lösung zu entscheiden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2096477</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2096477</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Fri, 22 Jul 2011 12:00:56 GMT</pubDate></item><item><title><![CDATA[Reply to ids einer map schnell ändern on Fri, 22 Jul 2011 12:05:37 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>Ist der entsprechende Abschnitt in deinem Code wirklich derart performancekritisch?</p>
<p>Falls es &quot;nur effizient&quot; sein soll, würde ich nicht lange überlegen, um zwischen einer standardkonformen und einer schnellen Lösung zu entscheiden.</p>
</blockquote>
<p>Ich bin eigentlich mit dem Gedanken hierher gekommen dass es dafür einen &quot;simplen&quot; standardkonformen Einzeiler gäbe den ich noch nicht kannte. Zumindest kam es mir wie eine alltägliche Aufgabe vor. Es ist weder kritisch noch notwendig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2096478</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2096478</guid><dc:creator><![CDATA[asdsad]]></dc:creator><pubDate>Fri, 22 Jul 2011 12:05:37 GMT</pubDate></item><item><title><![CDATA[Reply to ids einer map schnell ändern on Fri, 22 Jul 2011 13:21:48 GMT]]></title><description><![CDATA[<p>Reicht ein Zweizeiler? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<pre><code class="language-cpp">std::map&lt;int, Target&gt; shiftedIndex;
std::transform(original.begin(), original.end(), std::inserter(shiftedIindex, shiftedIndex.end()), [](std::pair&lt;int, Target&gt; const&amp; vt) { return std::make_pair(vt.first+10000, vt.second); });
</code></pre>
<p>siehe <a href="http://ideone.com/jaCyg#" rel="nofollow">http://ideone.com/jaCyg#</a></p>
<p>mal im Ernst: Ob man jetzt einen &quot;Einzeiler&quot; hat oder ob das durch eine selbstgeschrieben Funktion mit 3-4 Zeilen erledigt werden kann ist am Ende doch auch herzlich egal, oder?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2096518</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2096518</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Fri, 22 Jul 2011 13:21:48 GMT</pubDate></item><item><title><![CDATA[Reply to ids einer map schnell ändern on Fri, 22 Jul 2011 17:51:46 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>Reicht ein Zweizeiler? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<pre><code class="language-cpp">std::map&lt;int, Target&gt; shiftedIndex;
std::transform(original.begin(), original.end(), std::inserter(shiftedIindex, shiftedIndex.end()), [](std::pair&lt;int, Target&gt; const&amp; vt) { return std::make_pair(vt.first+10000, vt.second); });
</code></pre>
<p>siehe <a href="http://ideone.com/jaCyg#" rel="nofollow">http://ideone.com/jaCyg#</a></p>
<p>mal im Ernst: Ob man jetzt einen &quot;Einzeiler&quot; hat oder ob das durch eine selbstgeschrieben Funktion mit 3-4 Zeilen erledigt werden kann ist am Ende doch auch herzlich egal, oder?</p>
</blockquote>
<p>Wenn man ein guter Programmierer ist schon. Ich habe allerdings wenig Zeit und bin immer froh wenn so etwas schon in der stl abgeckt ist weil ich es selber wohl nicht besser hinkriege.</p>
<p>Danke für deinen Tipp. Sieht auch sehr interessant aus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2096646</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2096646</guid><dc:creator><![CDATA[asdsad]]></dc:creator><pubDate>Fri, 22 Jul 2011 17:51:46 GMT</pubDate></item></channel></rss>