I am working with data that shouldn't pop up twice. If it does, it should detect it and invoke a function handling that.
Currently, I am pushing some data to a vector and before insertion, it should check if the data is already contained in that vector. At the moment, this is not very effective, e.g
for (int i = 0; i < myVector.size() ; i++)
{
if ( myVector[i] == data )
{
// invoke function
return false;
}
}
I know set
is a special kind of vector which allows only unique data.
Is there another way to detect duplicate data being added (or at least attempting to add it) to the set
?
If you want to identify duplicates across the entire data set, then select the entire set. Navigate to the Home tab and select the Conditional Formatting button. In the Conditional Formatting menu, select Highlight Cells Rules. In the menu that pops up, select Duplicate Values.
A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction.
First let's just be clear that a set
is not a special kind of vector
. It's a kind of container, orthogonal to vector, that happens to prevent duplicates.
You can detect duplicates by checking the return value from insert
:
if(my_set.insert("value").second == false) { do_something_for_duplicate(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With