My answer suggests that when you say “subset”, you are really looking more for the equivalent of “substring”; that is, maintaining the order of the elements during the search.
Ultimately, I don't see how you could do this in less than O(n*m) . Given this, you can simply simply collapse your own:
template <typename T1, typename T2> bool contains(std::vector<T1> const& a, std::vector<T2> const& b) { for (typename std::vector<T1>::const_iterator i = a.begin(), y = a.end(); i != y; ++i) { bool match = true; typename std::vector<T1>::const_iterator ii = i; for (typename std::vector<T2>::const_iterator j = b.begin(), z = b.end(); j != z; ++j) { if (ii == a.end() || *j != *ii) { match = false; break; } ii++; } if (match) return true; } return false; }
demo version.
(You can probably be more creative with template options.)
Lightness races in orbit
source share