1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_myintersection(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)
{
InputIterator1 it1=first1;
InputIterator2 it2=first2;
bool stable;
while((it1!=last1)&&(it2!=last2))
{
do
{
stable=true;
while((it1!=last1)&&(it2!=last2)&&((*it1)<(*it2))){++it1; stable=false;}
while((it1!=last1)&&(it2!=last2)&&((*it2)<(*it1))){++it2; stable=false;}
}while((it1!=last1)&&(it2!=last2)&&(stable==false));
while((it1!=last1)&&(it2!=last2)&&(!((*it2)<(*it1))))
{
*result=*it1;
++result;
++it1;
}
}
return result;
}
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_mydifference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)
{
InputIterator1 it1=first1;
InputIterator2 it2=first2;
while((it1!=last1)&&(it2!=last2))
{
while((it1!=last1)&&(it2!=last2)&&((*it2)<(*it1))){++it2;}
while((it1!=last1)&&(it2!=last2)&&((*it1)<(*it2)))
{
*result=*it1;
++result;
++it1;
}
while((it1!=last1)&&(it2!=last2)&&(!((*it2)<(*it1)))){ ++it1; }
}
return result;
}
int main(int argc, char **argv)
{
int A1[] = {1, 3, 3, 5, 7, 9, 11};
int A2[] = {1, 1, 2, 3, 5, 8, 13};
const int N1 = sizeof(A1) / sizeof(int);
const int N2 = sizeof(A2) / sizeof(int);
cout << "Intersection of A1 and A2: ";
set_intersection(A1, A1 + N1, A2, A2 + N2, ostream_iterator<int>(cout, " "));
cout << endl;
cout << "Difference of A1 and A2: ";
set_difference(A1, A1 + N1, A2, A2 + N2,ostream_iterator<int>(cout, " "));
cout << endl;
return 0;
} |
Partager