i.e. all occurrences of 2 and 3 removed. I cannot run std::unique on the first container as I need element2 preserved. I need it in Standard Library so it will run as fast as possible.
The first thing I see is that your comp structure is incorrect. Also if you just added the operator() to your data structure you wouldn't need the comp structure.
From some documentation for std::set_difference():
The value returned indicates whether the first argument is considered to go before the second in the specific strict weak ordering it defines.
This means that you should be using the less than operator not the not equal to operator.
Lastly it would be helpful if you posted a small complete program that illustrates your problem.
A complete program is one that another user could copy and paste into a text file or internet compiler (e.g. http://cpp.sh ) and compile+run it without modification or errors (unless the compile errors are the point of the question).
data2 is not sorted, according to the criterion specified by comp. Therefore, the result of doing an asymmetric set different on [1, 2, 2, 2, 4, 4, 4, 5, 5] and [2, 4, 0, 0, 0, 0, 0, 0, 0] is undefined.
EDIT: Correction: elements 2 through 8 of data2 have undefined values. The array is in an undefined state of sortedness.