As the name says, in my head there are some ways to do this, but I donβt know which is faster.
So, let's say that we have: vector<int> vals with some values
1
After adding vals
sort(vals.begin(), vals.end()); auto last = unique(vals.begin(), vals.end()); vals.erase(last, vals.end());
2
Convert to setting after adding vals :
set<int> s( vals.begin(), vals.end() ); vals.assign( s.begin(), s.end() );
3
When I add my vals , I check if it is already in my vector:
if( find(vals.begin(), vals.end(), myVal)!=vals.end() ) // add my val
4
Use set from the beginning
Ok, I have these 4 methods, my questions are:
1 From 1, 2 and 3 , what is the fastest?
2 4 faster than the first 3?
3 With 2, after converting a vector to a set, is it more convenient to use the set to do what I need to do, or should I do vals.assign( .. ) and continue with my vector?