1

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.

12
  • 1
    You really got this from a book?? This looks dodgy -- s.sizeA= k; but k is not initialised, unless you have omitted something. Commented Jun 17, 2012 at 19:17
  • Look 5th line. @mathematician1975 Commented Jun 17, 2012 at 19:18
  • So what is the value of k then? Commented Jun 17, 2012 at 19:20
  • 1
    @Takarakaka : The 5th line declares k, but does not initialize it. Commented Jun 17, 2012 at 19:27
  • 1
    i, j and k are still not initialised with any values before they are used! Commented Jun 17, 2012 at 19:31

1 Answer 1

1

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 equal j++, s.arrayA[i++] = s1.arrayA[i++];

This code intersects to sets s1 and s2. It seems code suggests that arrays (that implement sets) are sorted. Code is walking on s1.arrayA and s2.arrayA and if some element is present in both sets, than it places that element in s.arrayA.

Sign up to request clarification or add additional context in comments.

5 Comments

OK, but how is it possible to do such a thing? It's not clear to me? What constructor is called? And why first j++ and only later i++?
It seems that Set s; calls default constructor for Set.
Aha, it is so much clear to me now. So why would I discard my first operator at all?
You cannot discard that operator, because it defines variable you use later. So is this code yours or from some book?)
From a book written by our local programmer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.