Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Edit</strong> In response to comment:</p> <blockquote> <p><sup><em>Remove duplicates function name is misleading, it is actually trying to return a list of words that are repeating in the sequence, but that result list only has one copy of each duplicate – <a href="https://stackoverflow.com/users/2624236/user2624236">user2624236</a> <a href="https://stackoverflow.com/questions/17891059/how-to-find-duplicates-in-a-stdvectorstrings-and-return-a-stdlist-of-them#comment26132135_17891059">10 hours ago</a></em></sup></p> </blockquote> <p>I hinted <code>std::sort</code> + <code>std::adjacent_find(... std::equal_to&lt;&gt;)</code>. Here's the realization of that:</p> <pre><code>template &lt;typename C, typename T = typename C::value_type&gt; std::list&lt;T&gt; adjacent_search(C input) { std::sort(begin(input), end(input)); static const auto eq = std::equal_to&lt;T&gt;{}; static const auto neq= std::not2(eq); std::list&lt;T&gt; dupes; auto end_streak = begin(input); auto dupe_at = std::adjacent_find(end_streak, end(input), eq); for(auto end_streak=begin(input); (dupe_at = std::adjacent_find(end_streak, end(input), eq)) != end(input); end_streak = std::adjacent_find(dupe_at, end(input), neq)) { dupes.insert(dupes.end(), *dupe_at); } return dupes; } </code></pre> <p>This implementation has several nice properties, such as a linear scan and reasonable worst case behaviour (e.g. if input contains 1000 duplicates of a single value, it won't do 1001 useless searches).</p> <p>However, the following (using a set) might be a lot simpler:</p> <pre><code>// simple, but horrific performance template &lt;typename C, typename T = typename C::value_type&gt; std::list&lt;T&gt; simple(C const&amp; input) { std::set&lt;T&gt; dupes; // optimization, dupes.find(x) in O(log n) for (auto it = begin(input); it != end(input); ++it) { if ((end(dupes) == dupes.find(*it))) // optimize by reducing find() calls &amp;&amp; (std::count(it, end(input), *it) &gt; 1)) { dupes.insert(dupes.end(), *it); } } return {begin(dupes), end(dupes)}; } </code></pre> <p>This will almost certainly perform better on smaller collections because there is less copying (except for the result). It could get rather bad worst case behaviour (for large inputs) because of the implicit linear search in <code>std::count</code>.</p> <p>I'd suggest you return the <code>std::set&lt;T&gt;</code> directly, instead of copying it to the list.</p> <p>Here's a test running <strong><a href="http://coliru.stacked-crooked.com/view?id=3f0a23a045ba69f6029fa54014857957-542192d2d8aca3c820c7acc656fa0c68" rel="nofollow noreferrer">Live on Coliru</a></strong> showing both versions.</p> <h2>Original answer</h2> <p>Now rather obsolete, because it didn't do what the OP wanted:</p> <pre><code>#include &lt;vector&gt; #include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;iterator&gt; int main() { std::vector&lt;std::string&gt; input = { "unsorted", "containing", "optional", "unsorted", "duplicate", "duplicate", "values" }; std::sort(begin(input), end(input)); std::unique_copy(begin(input), end(input), std::ostream_iterator&lt;std::string&gt;(std::cout, " ")); std::cout &lt;&lt; "\n"; } </code></pre> <p>Output:</p> <pre><code>containing duplicate optional unsorted values </code></pre> <p>See it live: <a href="http://coliru.stacked-crooked.com/view?id=f8cc78dbcce62ad276691b6541629a70-542192d2d8aca3c820c7acc656fa0c68" rel="nofollow noreferrer">http://coliru.stacked-crooked.com/view?id=f8cc78dbcce62ad276691b6541629a70-542192d2d8aca3c820c7acc656fa0c68</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload