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 : i...@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-ker...@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>

Reply via email to