I am trying to compare two vector objects, and return a single vector containing all the chars which appear in both vectors.
How would I go about this without writing some horribly complex manual method which compares every char in the first vector to every char in the second vector and using an if to add it to a third vector (which would be returned) if they match.
Maybe my lack of real experience with vectors is making me imagine this will be harder than it really is, but I suspect there is some simplier way which I have been unable to find through searching.
I think you’re looking for
std::set_intersection. The source vectors have to be sorted though. If you don’t care about the order of your output vector, you could always run it on sorted copies of your source vectors.And BTW, the manual naive way isn’t horribly complex. Given two source vectors
s1ands2, and a destination vectordest, you could write something that looks like this:You have a lot of options for the
findstep depending on your choice of data structure.