I like your algorithm. It's easy, readable and works!
In addition it is also fast. Using the sort approach, makes your time complexity around O(n*log(n)).
But can we go faster?
Well it all depends on your problem statement. Do you have to process extremely large input (Asymptotic complexity)?
I just want to show you another approach, where you do not use the built-in std functions and that runs in O(n). One might argue that it's not necessary in your case and that this approach brings other overheads for saving only log-n performance. Still, it might be useful to give an alternative view.
Let's show this with an example:
String1 = lolly
String2 = lolp
it should return that they have in common:
l = 2, o = 1
We could use two arrays/Maps. Depending if you are allowed to use built in datastructures. (If you only consider ASCII characters the arrays should be 128 integers long.)
You first run over the first string, which is done in O(n).
For each character in the string you add 1 to the representative in the first array/Map. You do the same for the second string and store this in the second array/Map.
this results in:
String1 => l = 3, o = 1, y = 1
String2 => l = 2, o = 1, p = 1
By taking the min() for each entry (considering absence of the char in one of the strings = 0), you end up with the common characters.
result => l = 2, o = 1, y = 0, p = 0
This all should be able to be done in 3 non-nested for loops, resulting in O(n)
What about the coding style?
First of all I have to compliment you on the use of
std::, (We discussed this question in chat. (Feel free to join us))
You should however consider using space after a comma:
std::set_intersection(std::begin(first), std::end(first), std::begin(second), std::end(second), std::begin(result));