<?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[Duplikate entfernen]]></title><description><![CDATA[<p>Einen sortierten Bereich unique zu machen, geht mit STL-Algorithmen sehr elegant:</p>
<p>v.resize(unique(v.begin(),v.end())-v.begin());</p>
<p>..aber gibt es auch eine schöne Lösung, jene Elemente komplett zu entfernen,<br />
die nicht unique sind? Beispiel [0,1,1,2,2,2,3] -&gt; [0,3]. Mir fällt nur ein,<br />
mit einer Schleife drüber zu laufen.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/299434/duplikate-entfernen</link><generator>RSS for Node</generator><lastBuildDate>Thu, 13 Aug 2026 06:12:29 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/299434.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 10 Feb 2012 19:31:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Duplikate entfernen on Fri, 10 Feb 2012 19:31:56 GMT]]></title><description><![CDATA[<p>Einen sortierten Bereich unique zu machen, geht mit STL-Algorithmen sehr elegant:</p>
<p>v.resize(unique(v.begin(),v.end())-v.begin());</p>
<p>..aber gibt es auch eine schöne Lösung, jene Elemente komplett zu entfernen,<br />
die nicht unique sind? Beispiel [0,1,1,2,2,2,3] -&gt; [0,3]. Mir fällt nur ein,<br />
mit einer Schleife drüber zu laufen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2179598</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2179598</guid><dc:creator><![CDATA[dupes]]></dc:creator><pubDate>Fri, 10 Feb 2012 19:31:56 GMT</pubDate></item><item><title><![CDATA[Reply to Duplikate entfernen on Fri, 10 Feb 2012 20:24:20 GMT]]></title><description><![CDATA[<p>ja klar<br />
alle in multiset eintragen, und dann die mit count==0 rausfiltern.</p>
<p><a href="http://www.cplusplus.com/reference/stl/multiset/count/" rel="nofollow">http://www.cplusplus.com/reference/stl/multiset/count/</a></p>
<p>Die Sortierung geht allerdings verloren, aber vielleicht kommt es daruf nicht an?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2179610</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2179610</guid><dc:creator><![CDATA[waldschrat918]]></dc:creator><pubDate>Fri, 10 Feb 2012 20:24:20 GMT</pubDate></item><item><title><![CDATA[Reply to Duplikate entfernen on Fri, 10 Feb 2012 20:56:42 GMT]]></title><description><![CDATA[<p>unique wird so oder ähnlich implementiert sein:</p>
<pre><code class="language-cpp">template &lt;class ForwardIterator&gt;
  ForwardIterator unique ( ForwardIterator first, ForwardIterator last )
{
  ForwardIterator result=first;
  while (++first != last)
  {
    if (!(*result == *first))  
      *(++result)=*first;
  }
  return ++result;
}
</code></pre>
<p>Das kannst du dir ja einfach mal anpassen, um das Gegenteil zu erreichen. Mit fertigen STL-Algorithmen alleine wirst du glaube ich nicht zum Ziel kommen, irgendwo wirst du ein bisschen eigenen Code schreiben müssen. Der Aufruf wird dann aber ähnlich elegant aussehen wie in deinem Beispiel für unique.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2179630</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2179630</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 10 Feb 2012 20:56:42 GMT</pubDate></item><item><title><![CDATA[Reply to Duplikate entfernen on Fri, 10 Feb 2012 21:44:27 GMT]]></title><description><![CDATA[<p>Mir ist mittlerweile was eingefallen:</p>
<p>Partitionieren mit unique, so daß zwei Bereiche entstehen. Dann mit<br />
set_difference den zweiten vom ersten abziehen. Sollte funktionieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2179651</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2179651</guid><dc:creator><![CDATA[dupes]]></dc:creator><pubDate>Fri, 10 Feb 2012 21:44:27 GMT</pubDate></item><item><title><![CDATA[Reply to Duplikate entfernen on Fri, 10 Feb 2012 22:09:05 GMT]]></title><description><![CDATA[<p><a href="http://www.cplusplus.com/reference/algorithm/unique/" rel="nofollow">http://www.cplusplus.com/reference/algorithm/unique/</a> : &quot;The elements past the new end of range are still valid, although with unspecified values.&quot; <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2179663</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2179663</guid><dc:creator><![CDATA[Dobi]]></dc:creator><pubDate>Fri, 10 Feb 2012 22:09:05 GMT</pubDate></item><item><title><![CDATA[Reply to Duplikate entfernen on Sat, 11 Feb 2012 00:07:05 GMT]]></title><description><![CDATA[<p>So etwa müsste das gehen:</p>
<pre><code class="language-cpp">#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;iterator&gt;

template&lt;typename iter_t&gt;
iter_t swap_ranges_fwd(iter_t first, iter_t last, iter_t first2) {
  using std::swap;

  while(first != last) {
    swap(*first, *first2);
    ++first;
    ++first2;
  }

  return first2;
}

template&lt;typename iter_t&gt;
iter_t keep_only_unique(iter_t first,
                        iter_t last)
{
  iter_t dest;

  dest = first = std::adjacent_find(first, last);

  while(first != last) {
    iter_t prev;

    do {
      prev = std::adjacent_find(first, last, std::not_equal_to&lt;typename std::iterator_traits&lt;iter_t&gt;::value_type&gt;());

      if(prev == last) {
        first = prev;
        break;
      } else {
        ++prev;
        first = std::adjacent_find(prev, last);
      }
    } while(prev == first);

    dest = swap_ranges_fwd(prev, first, dest);
  }

  return dest;
}

template&lt;typename T, std::size_t N&gt;
std::size_t array_size(T(&amp;)[N]) { return N; }

template&lt;typename T, std::size_t N&gt;
void print_uniques(T (&amp;arr)[N]) {
  std::copy(arr, keep_only_unique(arr, arr + N), std::ostream_iterator&lt;int&gt;(std::cout, &quot; &quot;));
  std::cout &lt;&lt; '\n';
}

int main() {
  int foo [] = { 1, 2, 2, 3, 4, 4, 4, 5, 5, 6, 7, 8, 8, 9, 9, 9, 10, 11, 12, 13, 13, 14, 14, 15 };
  int foo2[] = { 1, 2, 2, 3, 4, 4, 4, 5, 5, 6, 7, 8, 8, 9, 9, 9, 10, 11, 12, 13, 13, 14, 14, 14 };
  int foo3[] = { 1, 1, 1, 2, 3 };
  int foo4[] = { 1, 1, 1, 2, 2, 3 };
  int foo5[] = { 1, 1, 1, 2, 2 };

  print_uniques(foo);
  print_uniques(foo2);
  print_uniques(foo3);
  print_uniques(foo4);
  print_uniques(foo5);
}
</code></pre>
<p>Wahlweise statt swap_ranges_fwd auch std::copy, aber bei komplexeren Datentypen will man vielleicht unnötige Kopien vermeiden. Mit C++11 bietet sich hier natürlich Move-Semantik an. Die Standard-Funktionsvorlage std::swap_ranges verbietet sich überlappende Speicherbereiche als Parameter, also konnte ich sie hier leider nicht verwenden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2179690</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2179690</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Sat, 11 Feb 2012 00:07:05 GMT</pubDate></item><item><title><![CDATA[Reply to Duplikate entfernen on Sat, 11 Feb 2012 19:16:31 GMT]]></title><description><![CDATA[<p>Eine schöne Lösung. Danke.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2179923</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2179923</guid><dc:creator><![CDATA[dupes]]></dc:creator><pubDate>Sat, 11 Feb 2012 19:16:31 GMT</pubDate></item></channel></rss>