Since the data structure are something like
struct morph
{
size_t num;
std::string name;
//and so on
}
I have to find the different of the two sets with functor or some 3rd party
where could I find something like this?
Below is the implementation alter by me(refer to cplusplus.com)
template <class InputIterator1, class InputIterator2, class OutputIterator, typename binary_predicate>
OutputIterator set_difference_if (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
InputIterator2 last2, OutputIterator result, binary_predicate OP)
{
while (first1 != last1 && first2 != last2)
{
if ( OP(*first1, *first2) )
{
*result = *first1;
++result;
++first1;
}
else if ( OP(*first2, *first1) )
{
++first2;
}
else { ++first1; ++first2; }
}
return std::copy(first1, last1, result);
}
struct morph
{
size_t num;
};
int main()
{
std::array<morph, 5> A;
std::array<morph, 3> B;
for(size_t i = 0; i != A.size(); ++i) A[i].num = i;
B[0].num = 1; B[1].num = 3; B[2].num = 4;
std::vector<morph> C;
set_difference_if(A.begin(), A.end(), B.begin(), B.end(), std::back_inserter(C), [](morph const A, morph const B){ return A.num < B.num; } );
for(auto it = C.begin(); it != C.end(); ++it) std::cout<<it->num;
std::cout<<std::endl;
Is this code safe or not?If it is safe, I may refine the code later
The other way is use std::find_if to iterate every element and save those different
elements into vector too