Duplicate detection using dialing

I work with data that should not appear twice. If so, it should detect it and invoke the processing of the function.

I am currently pushing some data towards the vector and before inserting it, it must check whether the data is already contained in this vector. At the moment it is not very effective, for example

for (int i = 0; i < myVector.size() ; i++) 
{
  if ( myVector[i] == data ) 
  {
             // invoke function
             return false;
  }
}

I know that set- this is a special kind of vector that allows you to use only unique data.

Is there any other way to detect duplicate data being added (or at least trying to add it) in set?

+4
source share
4 answers

, a set vector. , , .

, insert:

if(my_set.insert("value").second == false) { do_something_for_duplicate(); }
+12

std::set std::pair<iterator, bool>, bool false, ( , ).

:

std::set<int> set{ 1, 2, 3 };
auto result = set.insert(1);
if (!result.second)
    std::cout << "Failed to insert element!" << std::endl;
+6

A std::set std::unordered_set - ++, vector... :

  • : ,
  • .
  • unordered_set , ( , , )

, (ref):

std::find(vector.begin(), vector.end(), item) != vector.end()

unordered_set insert , - boolean, , .

if (! my_set.insert(data).second) {
    // invoke function
    return false;
}
+3

std::unordered_set. insert, , , ( bool, , , false, ) .. .

+1

All Articles