删除两个数组交集

#include #include #include #include #include #include #include #include #include using namespace std; template OutputIterator delete_intersection(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator dest) { while (first1 != last1 && first2 != last2) { if (*first1 > *first2) { *dest = *first2; ++first2; ++dest; } else if (*first1 < *first2) { *dest = *first1; ++first1; ++dest; } else { ++first1; ++first2; } } // for (;first1 != last1; ++first1) *dest = *first1; for (;first2 != last2; ++first2) *dest = *first2; return dest; } int main() { int a[] = {1,1,2,2,5,6,9,9}; int b[] = {1,2,3,4,4,6,7,8,9,9,9,10}; vector vc; delete_intersection(a, a + sizeof(a)/sizeof(a[0]), b, b + sizeof(b)/sizeof(b[0]), back_inserter(vc)); std::copy(a, a + sizeof(a)/sizeof(a[0]), ostream_iterator(cout, ",")); cout << endl; std::copy(b, b + sizeof(b)/sizeof(b[0]), ostream_iterator(cout, ",")); cout << endl; std::copy(vc.begin(), vc.end(), ostream_iterator(cout, ",")); cout << endl; ::system("PAUSE"); return EXIT_SUCCESS; }

你可能感兴趣的:(数据结构算法)