<?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[Inhalt zweier Container auf Doubletten untersuchen und fehlende auflisten.]]></title><description><![CDATA[<p>servus,</p>
<p>ich habe zwei container vom typ map&lt;string, string&gt;.<br />
Ich interessiere mich nur für den Key, also den ersten String. Inhalt des zweiten ist egal.</p>
<p>Die beiden maps enthalten &quot;fast&quot; das gleiche, also einige Keys sind sowohl in der einen wie auch in der anderen map drin.</p>
<p>Was ich gerne hätte ist eine Iteration über beide Maps, die mir folgende Ausagbe gibt:</p>
<pre><code>Foo ist nur links.
Bar ist in beiden.
Baz ist nur rechts.
Blub ist in beiden.
Blah ist in beiden.
Heuwägelchen ist nur links.
usw...
</code></pre>
<p>Das ist mein Ansatz:</p>
<pre><code>typedef std::map&lt;std::string, std::string&gt; smap;
int main()
{
    smap oldmap, newmap;
    put_stuff_in_maps();

    smap::const_iterator ni = newmap.begin();
    smap::const_iterator ne = newmap.end();
    smap::const_iterator oi = oldmap.begin();
    smap::const_iterator oe = oldmap.end();
    for (;;)
    {
        while (ni != ne &amp;&amp; ni-&gt;first &lt; oi-&gt;first)
        {
            std::cerr &lt;&lt; ni-&gt;first &lt;&lt; &quot; only in new data.&quot; &lt;&lt; std::endl;
            ++ni;
        }
        while ((ni!=ne &amp;&amp; oi!=oe) &amp;&amp; oi-&gt;first == ni-&gt;first)
        {
            std::cout &lt;&lt; ni-&gt;first &lt;&lt; &quot; both in old and new data.&quot; &lt;&lt; std::endl;
            ++ni; ++oi;
        }
        if (ni == ne)
            break;
        while (oi != oe &amp;&amp; oi-&gt;first &lt; ni-&gt;first)
        {
            std::cerr &lt;&lt; oi-&gt;first &lt;&lt; &quot; only in old data.&quot; &lt;&lt; std::endl;
            ++oi;
        }
        if (oi == oe)
            break;
    }

    return EXIT_SUCCESS;
}
</code></pre>
<p>Funktioniert auch schon beinahe. Mein Problem ist, ich krieg die Abbruchbedingungen nie so hin dass er sowohl beide maps bis zum Ende abarbeitet als auch nicht in eine Endlosschleife hängen bleibt als auch nicht zwischendurch beim stringcompare segfaulted, weil ein iterator schon auf end() steht.<br />
Die obere variante segfaultet zwar nicht und bleibt auch nicht in einer endlos-loop hängen, aber ich hab das Gefühl, sie bricht manchmal zu früh ab.<br />
Wer findet den Fehler?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/316646/inhalt-zweier-container-auf-doubletten-untersuchen-und-fehlende-auflisten</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Jul 2026 17:15:47 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/316646.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 11 May 2013 15:03:18 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Inhalt zweier Container auf Doubletten untersuchen und fehlende auflisten. on Sat, 11 May 2013 15:03:18 GMT]]></title><description><![CDATA[<p>servus,</p>
<p>ich habe zwei container vom typ map&lt;string, string&gt;.<br />
Ich interessiere mich nur für den Key, also den ersten String. Inhalt des zweiten ist egal.</p>
<p>Die beiden maps enthalten &quot;fast&quot; das gleiche, also einige Keys sind sowohl in der einen wie auch in der anderen map drin.</p>
<p>Was ich gerne hätte ist eine Iteration über beide Maps, die mir folgende Ausagbe gibt:</p>
<pre><code>Foo ist nur links.
Bar ist in beiden.
Baz ist nur rechts.
Blub ist in beiden.
Blah ist in beiden.
Heuwägelchen ist nur links.
usw...
</code></pre>
<p>Das ist mein Ansatz:</p>
<pre><code>typedef std::map&lt;std::string, std::string&gt; smap;
int main()
{
    smap oldmap, newmap;
    put_stuff_in_maps();

    smap::const_iterator ni = newmap.begin();
    smap::const_iterator ne = newmap.end();
    smap::const_iterator oi = oldmap.begin();
    smap::const_iterator oe = oldmap.end();
    for (;;)
    {
        while (ni != ne &amp;&amp; ni-&gt;first &lt; oi-&gt;first)
        {
            std::cerr &lt;&lt; ni-&gt;first &lt;&lt; &quot; only in new data.&quot; &lt;&lt; std::endl;
            ++ni;
        }
        while ((ni!=ne &amp;&amp; oi!=oe) &amp;&amp; oi-&gt;first == ni-&gt;first)
        {
            std::cout &lt;&lt; ni-&gt;first &lt;&lt; &quot; both in old and new data.&quot; &lt;&lt; std::endl;
            ++ni; ++oi;
        }
        if (ni == ne)
            break;
        while (oi != oe &amp;&amp; oi-&gt;first &lt; ni-&gt;first)
        {
            std::cerr &lt;&lt; oi-&gt;first &lt;&lt; &quot; only in old data.&quot; &lt;&lt; std::endl;
            ++oi;
        }
        if (oi == oe)
            break;
    }

    return EXIT_SUCCESS;
}
</code></pre>
<p>Funktioniert auch schon beinahe. Mein Problem ist, ich krieg die Abbruchbedingungen nie so hin dass er sowohl beide maps bis zum Ende abarbeitet als auch nicht in eine Endlosschleife hängen bleibt als auch nicht zwischendurch beim stringcompare segfaulted, weil ein iterator schon auf end() steht.<br />
Die obere variante segfaultet zwar nicht und bleibt auch nicht in einer endlos-loop hängen, aber ich hab das Gefühl, sie bricht manchmal zu früh ab.<br />
Wer findet den Fehler?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322641</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322641</guid><dc:creator><![CDATA[nicht1337]]></dc:creator><pubDate>Sat, 11 May 2013 15:03:18 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt zweier Container auf Doubletten untersuchen und fehlende auflisten. on Sat, 11 May 2013 15:09:56 GMT]]></title><description><![CDATA[<p>Schaue dir mal <a href="http://en.cppreference.com/w/cpp/algorithm/mismatch" rel="nofollow">std::mismatch</a> an.<br />
Du musst ledliglich einen Prädikat übergeben, was aus == != macht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322643</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322643</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 11 May 2013 15:09:56 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt zweier Container auf Doubletten untersuchen und fehlende auflisten. on Sat, 11 May 2013 15:14:51 GMT]]></title><description><![CDATA[<p>ich suche den Fehler nicht. Aber alles wird viel einfacher, wenn du pro for-loop iteration jeden iterator maximal einmal incrementierst, abbrichst wenn einer der iteratoren am Ende ist und zum schluss noch die elemente der anderen map ausgibst</p>
<p>und ich hoffe, in deinem code sind die Namen ursprünglich hübscher.</p>
<pre><code>while(ni != ne &amp;&amp; oi != oe){
     if(ni-&gt;first &lt; oi-&gt;first){
         std::cerr &lt;&lt; ni-&gt;first &lt;&lt; &quot; only in new data.&quot; &lt;&lt; std::endl;
         ++ni;
     }
     else if(oi-&gt;first &lt; ni-&gt;first){
         std::cerr &lt;&lt; oi-&gt;first &lt;&lt; &quot; only in old data.&quot; &lt;&lt; std::endl;
         ++oi;
     }
     else{
         std::cout &lt;&lt; ni-&gt;first &lt;&lt; &quot; both in old and new data.&quot; &lt;&lt; std::endl;
         ++ni; ++oi;
     }
}
for(;ni != ne;++ni){
     std::cerr &lt;&lt; ni-&gt;first &lt;&lt; &quot; only in new data.&quot; &lt;&lt; std::endl;
}
while(oi != oe){
    std::cerr &lt;&lt; oi-&gt;first &lt;&lt; &quot; only in old data.&quot; &lt;&lt; std::endl;
    ++oi;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2322645</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322645</guid><dc:creator><![CDATA[otze]]></dc:creator><pubDate>Sat, 11 May 2013 15:14:51 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt zweier Container auf Doubletten untersuchen und fehlende auflisten. on Sat, 11 May 2013 15:30:04 GMT]]></title><description><![CDATA[<p>otze schrieb:</p>
<blockquote>
<pre><code>for(;ni != ne;++ni){
     std::cerr &lt;&lt; ni-&gt;first &lt;&lt; &quot; only in new data.&quot; &lt;&lt; std::endl;
}
while(oi != oe){
    std::cerr &lt;&lt; oi-&gt;first &lt;&lt; &quot; only in old data.&quot; &lt;&lt; std::endl;
    ++oi;
}
</code></pre>
</blockquote>
<p>Gespaltene Persönlichkeit?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322649</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322649</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sat, 11 May 2013 15:30:04 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt zweier Container auf Doubletten untersuchen und fehlende auflisten. on Sat, 11 May 2013 15:35:56 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p>otze schrieb:</p>
<blockquote>
<pre><code>for(;ni != ne;++ni){
     std::cerr &lt;&lt; ni-&gt;first &lt;&lt; &quot; only in new data.&quot; &lt;&lt; std::endl;
}
while(oi != oe){
    std::cerr &lt;&lt; oi-&gt;first &lt;&lt; &quot; only in old data.&quot; &lt;&lt; std::endl;
    ++oi;
}
</code></pre>
</blockquote>
<p>Gespaltene Persönlichkeit?</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /><br />
Und wieso eigentlich <code>std::cerr</code> ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322651</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322651</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sat, 11 May 2013 15:35:56 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt zweier Container auf Doubletten untersuchen und fehlende auflisten. on Sat, 11 May 2013 15:42:57 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p>Gespaltene Persönlichkeit?</p>
</blockquote>
<p>Nein, müde + copy&amp; paste.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322653</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322653</guid><dc:creator><![CDATA[otze]]></dc:creator><pubDate>Sat, 11 May 2013 15:42:57 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt zweier Container auf Doubletten untersuchen und fehlende auflisten. on Sat, 11 May 2013 16:31:19 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/5202">@otze</a> funktioniert einwandfrei, danke.</p>
<p>Std::cerr kam von mir, ich wollte die, die nicht doubletten sind, in der Konsole in rot haben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322664</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322664</guid><dc:creator><![CDATA[nicht1337]]></dc:creator><pubDate>Sat, 11 May 2013 16:31:19 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt zweier Container auf Doubletten untersuchen und fehlende auflisten. on Sun, 12 May 2013 18:54:03 GMT]]></title><description><![CDATA[<p>BTW: Ich finde die Lösung grundsätzlich gut (übersichtlich, verständlich), nur die letzten beiden Schleifen finde ich unnötig:</p>
<pre><code class="language-cpp">while (ni != ne || oi != oe)
{ 
      if (oi == oe || ni-&gt;first &lt; oi-&gt;first)
      { 
          std::cerr &lt;&lt; ni-&gt;first &lt;&lt; &quot; only in new data.&quot; &lt;&lt; std::endl; 
          ++ni; 
      } 
      else if (ni == ne || oi-&gt;first &lt; ni-&gt;first)
      { 
          std::cerr &lt;&lt; oi-&gt;first &lt;&lt; &quot; only in old data.&quot; &lt;&lt; std::endl; 
          ++oi; 
      } 
      else
      { 
          std::cout &lt;&lt; ni-&gt;first &lt;&lt; &quot; both in old and new data.&quot; &lt;&lt; std::endl; 
          ++ni; ++oi; 
      } 
}
</code></pre>
<p>DRY und so</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2322864</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2322864</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sun, 12 May 2013 18:54:03 GMT</pubDate></item></channel></rss>