I'm brand new to C++ and have been asked to convert a Java program to C++. I'm trying to write a method to check that all elements in an unordered_set exist in another unordered_set. I found the example below using hash_set but hash_set is deprecated and it is recommended to use unordered_set now.
// returns true if one contains all elements in two
bool SpecSet::containsAll(hash_set<Species*> one, hash_set<Species*> two) {
sort(one.begin(), one.end());
sort(two.begin(), two.end());
return includes(one.begin(), one.end(), two.begin(), two.end());
}
So I need a way to do this using unordered_set. Sort does not work on unordered sets and lookup speed is important, so I don't want to use an ordered set.
bool SpecSet::containsAll(unordered_set<Species*> one, unordered_set<Species*> two) {
return ?;
}
I'd really appreciate some help with an approach to doing this efficiently.
EDIT: I guess this will work. It seems there is no more efficient way but to loop over all in two.
bool SpecSet::containsAll(unordered_set<Species*> one, unordered_set<Species*> two) {
if(two.size() > one.size())
{
return false;
}
for(Species *species : two)
{
if(one.find(species) == one.end())
{
return false;
}
}
return true;
}