1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
#include <iostream>
#include <vector>
#include <iomanip>
#include <random>
#include <ctime>
#include <set>
int random_int( int min_value, int max_value )
{
static std::mt19937 twister( std::time(nullptr) ) ;
std::uniform_int_distribution<> distribution( min_value, max_value ) ;
return distribution(twister) ;
}
void ems_uniquefy(std::vector<int>& v, int rmin, int rmax)
{
typedef std::vector<int>::size_type vsize_t ;
if (v.size() < 2 )
return ;
std::set<int> processed ;
std::vector<vsize_t> dup_loc ;
processed.insert(v[0]) ;
for ( vsize_t i=1; i < v.size() ; ++i )
{
auto n = processed.size() ;
processed.insert(v[i]);
if ( n == processed.size() )
dup_loc.push_back(i) ;
}
for ( vsize_t i=0; i < dup_loc.size(); ++i )
{
auto n = processed.size() ;
int value ;
while ( n==processed.size())
{
value = random_int(rmin, rmax);
processed.insert(value) ;
}
v[dup_loc[i]] = value ;
}
}
std::ostream& print_on(std::ostream& os, std::vector<int> v)
{
for ( auto i = v.begin(); i< v.end() ; ++i )
{
os << std::setw(3) << *i ;
}
return os << '\n' ;
}
int main()
{
int arr1[20] = { 3, 7, 10, 20, 5, 3, 9, 8, 17, 19, 5, 10, 20, 11, 9, 2, 7, 2, 13 };
int arr2[20] = { 13, 8, 19, 2, 7, 10, 8, 20, 19, 3, 5, 2, 19, 10, 4, 13, 9, 10, 2 };
std::vector<int> v1(arr1, arr1+20) ;
std::vector<int> v2(arr2, arr2+20) ;
std::vector<int> v1_copy(v1) ;
std::vector<int> v2_copy(v2) ;
ems_uniquefy(v1, 1, 20) ;
ems_uniquefy(v2, 1, 20) ;
print_on(std::cout, v1_copy) ;
print_on(std::cout, v1) ;
std::cout << "\n\n" ;
print_on(std::cout, v2_copy) ;
print_on(std::cout, v2) ;
}
|