title: Returning unique() function in C++
body: <p>I came across the following function, which sorts an array passed down by main(), removes duplicates, and returns the  number of unique elements. It's the last bit I'm having a hard time wrapping my head around.</p>
  
  <pre><code>int reduce(long ar[], int n) {
    sort(ar, ar + n);
    return unique(ar, ar + n) - ar; // ??? 
  }
  </code></pre>
  
  <p>To my understanding unique() returns a pointer to the end of the segment that stores the  unique values in the array. But I don't see why subtracting the array name from the iterator results in an int that equals the number of unique elements, or why <code>unique(ar, ar+n)</code> can't be typecasted to int to achieve the same result.</p>