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);
}