Single elements <vector></vector>



  • How quickly do you know if there are the same elements in the vector?



  • There's a standard method for this. http://en.cppreference.com/w/cpp/algorithm/unique

    And that's how it works.

    set<int> s;
    unsigned size = vec.size();
    for( unsigned i = 0; i < size; ++i ) s.insert( vec[i] );
    vec.assign( s.begin(), s.end() );
    

    And so:

    set<int> s( vec.begin(), vec.end() );
    vec.assign( s.begin(), s.end() );
    

    If the number of duplicates is large, the productivity option from std:set will be more productive.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2