1

I created a function create() that generate 10 random numbers in a vector. And I am trying to create a function that removes duplicates. When I print the vector within the function remove_dup(), it works. But when I print it in the main function, it doesn't work. Please help. The other examples on here are not in a function.

#include <ctime>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <bits/stdc++.h>

using namespace std;


vector <int> listt;
vector <int> list2;
int number=0;
int create() {

    srand((unsigned) time(0));
  for (int i = 0; i < 10; i++) {
    number = (rand() % 20) + 1;
    listt.push_back(number);
  }
return 0;
}
void print(vector <int> a) {
   cout << "The vector elements are : ";

   for(int i=0; i < a.size(); i++)
        cout << a[i] << ' ';
}


vector <int> remove_dup(vector <int> listt) {
sort(listt.begin(), listt.end());
for(int i=0; i < listt.size(); i++){
        if (listt[i-1]==listt[i]){
            listt.erase(listt.begin()+i);
            i=0;

        }
}
return listt;

}

using namespace std;
int main() {
create();
  print(listt);
  cout<<endl;
  remove_dup(listt);
  print(listt);


}
dreamcrash
  • 36,542
  • 23
  • 64
  • 87
Ron Mercer
  • 49
  • 5
  • 1
    https://courses.washington.edu/css342/zander/css332/passby.html Quoting from the link: By definition, pass by value means you are making a copy in memory of the actual parameter's value that is passed in, a copy of the contents of the actual parameter. Use pass by value when when you are only "using" the parameter for some computation, not changing it for the client program. In pass by reference (also called pass by address), a copy of the address of the actual parameter is stored. Use pass by reference when you are changing the parameter passed in by the client program. – Roy2511 Dec 01 '20 at 06:34
  • 6
    `listt = remove_dup(listt);` in `main` would seemingly solve your problem, though I would suggest just using a reference argument instead if that's the only use of `remove_dup` in your program. Either way (but not both). That all these seem to be pumped about by value isn't very efficient regardless. – WhozCraig Dec 01 '20 at 06:38
  • 2
    Totally unrelated: `#include ` looks to be [cargo-cult](https://en.wikipedia.org/wiki/Cargo_cult_programming) behaviour the way it is used here. [You shouldn't use bits/stdc++.h](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h), but if you do, know that it includes pretty much the entire C++ Standard library making the bulk of the other includes pointless. – user4581301 Dec 01 '20 at 06:41
  • 1
    Apart from either of the corrections in the comments above your algorithm is bugged. Try testing with a vector of three equal elements. Basically if you erase an element you should not also increment `i` because you are making the vector one shorter and moving onto the next element, thereby skipping one element. If that element is the last in the vector it doesn't get tested. – john Dec 01 '20 at 06:42
  • @john thanks, I've added i=0, in my if statement. – Ron Mercer Dec 01 '20 at 07:03
  • @RonMercer That works but is horribly inefficient because you rescan the entire vector after each duplicate, `i--` is better. When you erase an element you should not increment `i`, so doing `i--` will cancel out the `i++` at the end of the loop. – john Dec 01 '20 at 07:05
  • See https://en.cppreference.com/w/cpp/algorithm/unique for that algorithm, also that page comes with examples and possible implementations :) – yussuf Dec 01 '20 at 07:13
  • 2
    Also, you must not start the loop at `i=0` because you access the element at `i-1`. – j6t Dec 01 '20 at 07:27

0 Answers0