title: STL Compliant iterator for two arrays
body: <p>I want to sort two arrays a and b of the same size in the following way: Array b is ordered the same way array a is being sorted. Example input: </p>
  
  <pre><code>a = {3, 1, 5}
  b = {2, 6, 4}
  </code></pre>
  
  <p>Example out</p>
  
  <pre><code>a = {1, 3, 5}
  b = {6, 2, 4}
  </code></pre>
  
  <p>Such that the values of b are irrelevant for their reordering, but instead follow the reordering of array a.</p>
  
  <p>I want to use stl::sort to do this, and i need to do it as efficiently as possible. So I don't want to copy all elements into structs or order an array with the index which i afterwards could use to order the arrays a and b. What i thought would be the least overhead should be a RandomAccessIterator (since sort requires random access). Now my problem is, I'm really not that good in C++. If someone could give me hints on a level a noob could understand i'd be delighted. I've found some solutions: </p>
  
  <p><a href="https://stackoverflow.com/questions/3909272/sorting-two-corresponding-arrays">Sorting two corresponding arrays</a>, but both proposed solutions don't seem performant enough, </p>
  
  <p><a href="http://www.stanford.edu/~dgleich/notebook/2006/03/sorting_two_arrays_simultaneou.html" rel="nofollow noreferrer">http://www.stanford.edu/~dgleich/notebook/2006/03/sorting_two_arrays_simultaneou.html</a> which uses boost stuff which I assume breaks the compliance with stl (also honestly I don't understand all the template stuff used there, i have a double and an int array and the size n of both, so i don't think i need templates), and finally this </p>
  
  <p><a href="http://www.c-plusplus.de/forum/313698-full" rel="nofollow noreferrer">http://www.c-plusplus.de/forum/313698-full</a> solution where i got stuck at the point that i don't know how to implement operator-> and operator* since i don't know if i can return only one value (the value from array a), i.e. if this value is used only for comparison or also for assigning values. also the solution in this thread compares the pointer values for the comparison operators, which i'm not sure is correct (shouldn't it be the values behind the pointers?).</p>
  
  <p>Here's what i have so far, if you see horrible noob mistakes pls tell me where i went wrong :)</p>
  
  <pre><code>#include "DoubleArrayRAccIterator.h"
  
  
  DoubleArrayRAccIterator::~DoubleArrayRAccIterator() {
      // TODO Auto-generated destructor stub
  }
  struct doubleValue{
      double* a_val;
      int* b_val;
  };
  
  double::DoubleArrayRAccIterator::DoubleArrayRAccIterator(double&amp; a_arr,int&amp; b_arr, int size) {
      a_begin = &amp;a_arr;
      b_begin = &amp;b_arr;
      a = &amp;a_arr;
      b = &amp; b_arr;
      n = size;
  }
  
  DoubleArrayRAccIterator::DoubleArrayRAccIterator() {
      a = 0;
      b = 0;
      n = 0;
  }
  
  DoubleArrayRAccIterator::DoubleArrayRAccIterator(const DoubleArrayRAccIterator&amp; it) {
      a = it.a;
      b = it.b;
      n = it.n;
  }
  
  DoubleArrayRAccIterator&amp; DoubleArrayRAccIterator::operator=(const DoubleArrayRAccIterator&amp; it) {
      a = it.a;
      b = it.b;
      n = it.n;
      return *this;
  }
  
  DoubleArrayRAccIterator&amp; DoubleArrayRAccIterator::operator++() {
      ++a;
      ++b;
      return *this;
  }
  
  
  DoubleArrayRAccIterator&amp; DoubleArrayRAccIterator::operator--() {
      --a;
      --b;
      return *this;
  }
  
  DoubleArrayRAccIterator DoubleArrayRAccIterator::operator++(int) {
      DoubleArrayRAccIterator it(*this);
      ++a;
      ++b;
      return it;
  }
  
  
  DoubleArrayRAccIterator DoubleArrayRAccIterator::operator--(int) {
      --a;
      --b;
      return *this;
  }
  
  DoubleArrayRAccIterator&amp; DoubleArrayRAccIterator::operator+=(diff_type x) {
      a += x;
      b += x;
      return *this;
  }
  
  
  DoubleArrayRAccIterator&amp; DoubleArrayRAccIterator::operator-=(diff_type x) {
      a += x;
      b += x;
      return *this;
  }
  
  DoubleArrayRAccIterator DoubleArrayRAccIterator::operator+(diff_type x) const {
      a += x;
      b += x;
      return *this;
  }
  
  
  typename DoubleArrayRAccIterator DoubleArrayRAccIterator::operator-(diff_type x) const {
      a -= x;
      b -= x;
      return *this;
  }
  
  
  DoubleArrayRAccIterator DoubleArrayRAccIterator::operator+(const DoubleArrayRAccIterator&amp; it) const {
      a += it.a;
      b += it.b;
      return *this;
  }
  
  
  DoubleArrayRAccIterator DoubleArrayRAccIterator::operator-(const DoubleArrayRAccIterator&amp; it) const {
      a -= it.a;
      b -= it.b;
      return *this;
  }
  
  
  DoubleArrayRAccIterator::reference DoubleArrayRAccIterator::operator*() const {
      // this MUST be wrong, only return value of array a?
      doubleValue result;
      result.a_val=a;
      result.b_val=b;
      return *result;
  }
  
  DoubleArrayRAccIterator::pointer DoubleArrayRAccIterator::operator-&gt;() const {
      // this MUST be wrong, only return value of array a?
      doubleValue result;
      result.a_val=a;
      result.b_val=b;
      return &amp;result;
  }
  
  
  DoubleArrayRAccIterator::reference DoubleArrayRAccIterator::operator[](diff_type x) const {
      // this MUST be wrong, only return value of array a?
      doubleValue result;
      result.a_val=a_begin+x;
      result.b_val=b_begin+x;
      return *result;
  }
  
  
  bool DoubleArrayRAccIterator::operator==(const DoubleArrayRAccIterator&amp; it) const {
      return a == it.a;
      //compare indices or values here?
  }
  
  
  bool DoubleArrayRAccIterator::operator!=(const DoubleArrayRAccIterator&amp; it) const {
      return a != it.a;
      //compare indices or values here?
  }
  
  
  bool DoubleArrayRAccIterator::operator&lt;(const DoubleArrayRAccIterator&amp; it) const {
      //compare indices or values here?
  }
  
  
  bool DoubleArrayRAccIterator::operator&gt;(const DoubleArrayRAccIterator&amp; it) const {
      //compare indices or values here?
  }
  
  
  bool DoubleArrayRAccIterator::operator&lt;=(const DoubleArrayRAccIterator&amp; it) const {
      //compare indices or values here?
  }
  
  
  bool DoubleArrayRAccIterator::operator&gt;=(const DoubleArrayRAccIterator&amp; it) const {
      //compare indices or values here?
  }
  
  
  DoubleArrayRAccIterator begin() {
      return DoubleArrayRAccIterator(a_begin, b_begin, n);
  }
  
  
  DoubleArrayRAccIterator end() {
      return DoubleArrayRAccIterator(a_begin + n, b_begin + n, n);
  }
  </code></pre>
  
  <p>And if anyone didn't stop reading yet and still has patience, i'm confused by the "diff_type", "reference" and "pointer" types i just copied from here <a href="http://www.c-plusplus.de/forum/313698-full" rel="nofollow noreferrer">http://www.c-plusplus.de/forum/313698-full</a>, what are they supposed to be?</p>