I am reading some book and I have encountered a piece of code that wasn’t explained in the book, but has some part which is very confusing to me, bold part, and I want to know what is it about.
void Set::intersection(const Set& s1, const Set& s2)
{
Set s;
s.arrayA = new double[ s1.sizeA<s2.sizeA ? s1.sizeA : s2.sizeA];
int i, j, k;
while(i < s1.sizeA && j < s2.sizeA)
if(s1.arrayA[i] < s2.arrayA[j])
i++;
else if (s1.arrayA[i] > s2.arrayA[j])
j++;
else
s.arrayA[k++] = s1.arrayA[j++,i++]; // question is about this line
s.sizeA= k;
deleteA();
copyA(s);
}
What does it do, and why is there two parameters inside the [] brackets? Thanks in advance.
Two parameter within brackets is expression using comma operator. Result of such expression is result of last item (
j++, i++gives i incremented by one, while j is also incremented by one).So
s.arrayA[i++] = s1.arrayA[j++,i++];really can be converted to equalj++, s.arrayA[i++] = s1.arrayA[i++];This code intersects to sets
s1ands2. It seems code suggests that arrays (that implement sets) are sorted.Code is walking on
s1.arrayAands2.arrayAand if some element is present in both sets, than it places that element ins.arrayA.