在STL中关于有序序列有这么四个算法:
set_union(beg, end, beg, end2, dest); //求并集A∪B
set_union(beg, end, beg, end2, dest, comp);
< InputIterator1, InputIterator2, OutputIterator>
OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
()
{
(first1==last1) std::copy(first2,last2,result);
(first2==last2) std::copy(first1,last1,result);
(*first1<*first2) { *result = *first1; ++first1; }
(*first2<*first1) { *result = *first2; ++first2; }
{ *result = *first1; ++first1; ++first2; }
++result;
}
}
set_intersection(beg, end, beg, end2, dest); //求交集A∩B
set_intersection(beg, end, beg, end2, dest, comp);
< InputIterator1, InputIterator2, OutputIterator>
OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
(first1!=last1 && first2!=last2)
{
(*first1<*first2) ++first1;
(*first2<*first1) ++first2;
{
*result = *first1;
++result; ++first1; ++first2;
}
}
result;
}
set_difference(beg, end, beg, end2, dest); //求差集A-B
set_difference(beg, end, beg, end2, dest, comp);
< InputIterator1, InputIterator2, OutputIterator>
OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
(first1!=last1 && first2!=last2)
{
(*first1<*first2) { *result = *first1; ++result; ++first1; }
(*first2<*first1) ++first2;
{ ++first1; ++first2; }
}
std::copy(first1,last1,result);
}
set_symmetric_difference(beg, end, beg, end2, dest); //求对称差A∪B-A∩B
set_symmetric_difference(beg, end, beg, end2, dest, comp);
< InputIterator1, InputIterator2, OutputIterator>
OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
()
{
(first1==last1) std::copy(first2,last2,result);
(first2==last2) std::copy(first1,last1,result);
(*first1<*first2) { *result=*first1; ++result; ++first1; }
(*first2<*first1) { *result = *first2; ++result; ++first2; }
{ ++first1; ++first2; }
}
}
所以能够直接用这样的stl的算法解决。假设一次无法把A文件读入内存,能够分块读取,多处理几次。