Using std::set to find duplicate values

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?)
Last edited on
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
#include <iostream>
#include <set>
#include <vector>
#include <ctime>
#include <cstdlib>

int main()
{

	std::vector <int> myVec;

	srand(time(NULL));
	unsigned int randomVal [3];

	for (unsigned int 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 :\
Last edited on
If the range of numbers is not too large:

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
#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' ;
    }
}

http://coliru.stacked-crooked.com/a/22c79db1fc5e6422
I managed to get it working by writing the following code,

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
#include <iostream>
#include <set>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <string>
#include <algorithm>

int main()
{
	std::vector <int> myVec;
	std::vector<int>::iterator it;

	srand(time(NULL));

	for (int i = 0; i < 100000; i++){

	unsigned int randomVal [3];

	for (unsigned int i = 0; i < 3; i++){
		randomVal[i] = (rand()%100+1);
		myVec.push_back (randomVal[i]);
	}

	std::sort (myVec.begin(), myVec.end()); 

	for (int i = 0; i < 3; i++) {
		std::cout << myVec[i] << " ";
	}

	std::cout << std::endl;

	it = std::adjacent_find (myVec.begin(),myVec.end());

	
	if (it!=myVec.end()){
		std::cout << "Replicated elements: " << *it << std::endl;
	}

	int dupeVal = *it; //Holds the value of whatever the replicated elements are
		
	myVec.erase (myVec.begin(), myVec.end());

	std::cin.get();
	}
}
Last edited on
Topic archived. No new replies allowed.