input should be a list of numbers from 1 to 9 generated randomly that can repeat
output should be a list of numbers from 1 to 9 that do not repeat
input: 1,3,4,2,1,5,5,9,8
output: 3,1,4,2,5,9,8
If this is the case Phil, then you need to be generating a random set of integers from 1 to 9, not a sequence of integers from 1 to 9:
// v is always: 1 2 3 4 5 6 7 8 9 (no duplicates, ever)
for(int i=1;i<10;i++)
v.push_back(i);
random_shuffle(v.begin(),v.end()); // (elements might be in a random order, but there still aren't any duplicates)
// v could be: 9 1 8 6 7 4 6 3 2
for(int i=1;i<10;i++)
v.push_back( rand()%9 + 1 ); // (may have duplicates)
(See the reference on
rand.
rand() returns an integer between 0 and
RAND_MAX -- usually 32767 -- and the modulus wraps that around the range between 0 and 8, and adding one brings it between 1 and 9.)
Then, you'll need to include
algorithm, such that you can use
sort to move duplicates together, then use
unique to move the duplicate values to the back. Once the unique values are in front, you can remove the duplicates from
v by using the iterator returned by
unique:
sort( v.begin(), v.end() );
v.erase( unique(v.begin(),v.end()), v.end() );
Edit:
Hurr durr, I read can documentation good.
Edited by fastcall22, 16 November 2012 - 12:03 PM.