public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/106869] New: ranges::unique does nothing unless ranges::sort called first or something else is wrong.
@ 2022-09-07 10:44 info at kemalakcam dot com
  2022-09-07 11:25 ` [Bug libstdc++/106869] " redi at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: info at kemalakcam dot com @ 2022-09-07 10:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106869

            Bug ID: 106869
           Summary: ranges::unique does nothing unless ranges::sort called
                    first or something else is wrong.
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: info at kemalakcam dot com
  Target Milestone: ---

ka@localhost:~/projects/ka_projects/ka_examples_cpp20/ka_example_ranges_unique$
cat ka_example_ranges_unique.cpp
// 20220622T0300 : Kemal Akcam : info@kemalakcam.com :
ka_example_ranges_unique.cpp
// compile below code with gcc -lstdc++ -std=c++20 ka_example_ranges_unique.cpp
-o ka_example_ranges_unique

#include <bits/stdc++.h>

using namespace std;

class c
{
 public:
 vector<string> v;
 void printVectorContent(void)
 {
  cout << "vector<string> v = ";
  for(int i=0; i < v.size(); i++)
   cout << v[i].c_str() << " ";
  cout << endl;
 };
};

int main(void)
{
 c *k = new c();
 k->v = {"aa", "ab", "ga", "ewr", "r42","rtwgh", "qwq","fasa", "ss","aa",
"awrq1", "aa"};

 k->printVectorContent();

 cout << "ranges::unique(v);" << endl;
 ranges::unique(k->v);
 k->printVectorContent();
 cout << "20220622T0300 : Kemal Akcam : ranges::unique did not function, there
are still 3 x 'aa' in vector" << endl;

 cout << "ranges::sort(v);" << endl;
 ranges::sort(k->v);
 k->printVectorContent();

 cout << "ranges::unique(v);" << endl;
 ranges::unique(k->v);
 k->printVectorContent();
 cout << "20220622T0300 : Kemal Akcam : ranges::unique did function after
running ranges::sort, there are not 3 x 'aa' in vector, only 1 x 'aa'" << endl;
}

ka@localhost:~/projects/ka_projects/ka_examples_cpp20/ka_example_ranges_unique$
gcc -lstdc++ -std=c++20 ka_example_ranges_unique.cpp -o
ka_example_ranges_unique

ka@localhost:~/projects/ka_projects/ka_examples_cpp20/ka_example_ranges_unique$
./ka_example_ranges_unique 
vector<string> v = aa ab ga ewr r42 rtwgh qwq fasa ss aa awrq1 aa 
ranges::unique(v);
vector<string> v = aa ab ga ewr r42 rtwgh qwq fasa ss aa awrq1 aa 
20220622T0300 : Kemal Akcam : ranges::unique did not function, there are still
3 x 'aa' in vector
ranges::sort(v);
vector<string> v = aa aa aa ab awrq1 ewr fasa ga qwq r42 rtwgh ss 
ranges::unique(v);
vector<string> v = aa ab awrq1 ewr fasa ga qwq r42 rtwgh ss   
20220622T0300 : Kemal Akcam : ranges::unique did function after running
ranges::sort, there are not 3 x 'aa' in vector, only 1 x 'aa'

ka@localhost:~/projects/ka_projects/ka_examples_cpp20/ka_example_ranges_unique$
lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 10 (buster)
Release:        10
Codename:       buster

ka@localhost:~/projects/ka_projects/ka_examples_cpp20/ka_example_ranges_unique$
cat /proc/version 
Linux version 4.19.0-13-amd64 (debian-kernel@lists.debian.org) (gcc version
8.3.0 (Debian 8.3.0-6)) #1 SMP Debian 4.19.160-2 (2020-11-28)

ka@localhost:~/projects/ka_projects/ka_examples_cpp20/ka_example_ranges_unique$
gcc --version
gcc (GCC) 12.1.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Adding below instead of #include <bits/stdc++.h> gives same result.
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug libstdc++/106869] ranges::unique does nothing unless ranges::sort called first or something else is wrong.
  2022-09-07 10:44 [Bug c++/106869] New: ranges::unique does nothing unless ranges::sort called first or something else is wrong info at kemalakcam dot com
@ 2022-09-07 11:25 ` redi at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: redi at gcc dot gnu.org @ 2022-09-07 11:25 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106869

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Yes, that's correct.

std::unique and std::ranges::unique only remove **consecutive** duplicates,
they don't search the entire range again and again for duplicates. So you need
to sort it first, or otherwise arrange for all duplicates to be adjacent to
each other.

https://en.cppreference.com/w/cpp/algorithm/unique

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-09-07 11:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-07 10:44 [Bug c++/106869] New: ranges::unique does nothing unless ranges::sort called first or something else is wrong info at kemalakcam dot com
2022-09-07 11:25 ` [Bug libstdc++/106869] " redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).