std::sort(v1.begin(), v1.end()); std::sort(v2.begin(), v2.end()); std::vector<string> v3; std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(v3));
Or if you do not want to sort:
std::set<string> s1(v1.begin(), v1.end()); std::set<string> s2(v2.begin(), v2.end()); std::vector<string> v3; std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), std::back_inserter(v3));
You might want to use a multiset if there might be duplicates in the vector.
Erik Mar 07 2018-11-22T00: 00Z
source share