I think using std::set is the way to go about this although I have no experience with it and cant find any basic examples using it. If I have a vector with 3 values 6,7,7 (this will change per loop, 20,34,55 / 1,2,3 / 4,5,5) I just want to be able to check my vector and if it contains unique values do nothing , if contains duplicated values (using set, 7 is the duplicate in the first example) then have a new variable (vector/iterator?) that contains the duplicate value and then the same if all 3 are duplicated.
What is the best way to do this using std::set (if that is the best way?)
#include <iostream>
#include <set>
#include <vector>
#include <ctime>
#include <cstdlib>
int main()
{
std::vector <int> myVec;
srand(time(NULL));
unsignedint randomVal [3];
for (unsignedint i = 0; i < 3; i++){
randomVal[i] = (rand()%100+1);
myVec.push_back (randomVal[i]);
}
for (int i = 0; i < 3; i++) {
std::cout << myVec[i] << " ";
}
std::cout << std::endl;
std::set<int> s(myVec.begin(),myVec.end());
std::cout << s; //doesnt work, doesnt work with s[0] either not sure how to get
//it to cout or use if statements based on S value
std::cin.get();
}
as with the comment , I think this should put any duplicate values in s but really not sure how to get this working as the complier doesnt like it , seems like it should be simple though :\
#include <vector>
#include <numeric>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
// generate a vector containing n unique random integers in [ min_value, max_value ]
std::vector<int> unique_rand( std::size_t n, int min_value, int max_value )
{
std::vector<int> vec( max_value - min_value + 1 ) ;
// fill it up with min_value, min_value+1, min_value+2, ... max_value
std::iota( std::begin(vec), std::end(vec), min_value ) ;
// generate a random permutation of these numbers
std::random_shuffle( std::begin(vec), std::end(vec) ) ;
// resize to n elements (pick the first 'n' integers)
vec.resize(n) ;
return vec ;
}
int main()
{
std::srand( std::time(nullptr) ) ;
for( int i = 3 ; i < 10 ; ++i )
{
for( int r : unique_rand( 3, i, i*i ) ) std::cout << r << ' ' ;
std::cout << '\n' ;
}
}