0

Does the STD library provide any function that allows me to find intersection of two std::hash_set?

I tried using std::set_intersection but I get assertion error with Expression: sequence not ordered, is there any work around this?

#include <hash_set>
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
int main()
{
    std::hash_set<std::string> h1;
    h1.insert("NoMainDraw");
    h1.insert("NoLight");
    h1.insert("Random");

    std::hash_set<std::string> h2;
    h2.insert("NoLight");

    std::vector<std::string> groupDifference;
    std::set_intersection(h1.cbegin(), h1.cend(),
                          h2.cbegin(), h2.cend(),
                          std::back_inserter(groupDifference));

    for(const std::string& i : groupDifference)
    {
        std::cout << i << std::endl;
    }
}
David G
  • 94,763
  • 41
  • 167
  • 253
Caesar
  • 9,483
  • 8
  • 40
  • 66
  • 1
    If you need `set_intersection`, you probably want to use `std::set` instead (and you don't want to use `hash_set` in any case--the standard version is `std::unordered_set`. – Jerry Coffin Aug 13 '14 at 02:05
  • `set_intersection` only works on sorted ranges. – PaulMcKenzie Aug 13 '14 at 02:06
  • 1
    [tr1::unordered_set union and intersection](http://stackoverflow.com/questions/896155/tr1unordered-set-union-and-intersection) may help. – jweyrich Aug 13 '14 at 02:09

1 Answers1

1

You'll have to iterate one map doing lookups in the other. There's no Standard-library algo.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252