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:
a = {3, 1, 5}
b = {2, 6, 4}
Example out
a = {1, 3, 5}
b = {6, 2, 4}
Such that the values of b are irrelevant for their reordering, but instead follow the reordering of array a.
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:
Sorting two corresponding arrays, but both proposed solutions don't seem performant enough,
http://www.stanford.edu/~dgleich/notebook/2006/03/sorting_two_arrays_simultaneou.html 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
http://www.c-plusplus.de/forum/313698-full 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?).
Here's what i have so far, if you see horrible noob mistakes pls tell me where i went wrong :)
#include "DoubleArrayRAccIterator.h"
DoubleArrayRAccIterator::~DoubleArrayRAccIterator() {
// TODO Auto-generated destructor stub
}
struct doubleValue{
double* a_val;
int* b_val;
};
double::DoubleArrayRAccIterator::DoubleArrayRAccIterator(double& a_arr,int& b_arr, int size) {
a_begin = &a_arr;
b_begin = &b_arr;
a = &a_arr;
b = & b_arr;
n = size;
}
DoubleArrayRAccIterator::DoubleArrayRAccIterator() {
a = 0;
b = 0;
n = 0;
}
DoubleArrayRAccIterator::DoubleArrayRAccIterator(const DoubleArrayRAccIterator& it) {
a = it.a;
b = it.b;
n = it.n;
}
DoubleArrayRAccIterator& DoubleArrayRAccIterator::operator=(const DoubleArrayRAccIterator& it) {
a = it.a;
b = it.b;
n = it.n;
return *this;
}
DoubleArrayRAccIterator& DoubleArrayRAccIterator::operator++() {
++a;
++b;
return *this;
}
DoubleArrayRAccIterator& 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& DoubleArrayRAccIterator::operator+=(diff_type x) {
a += x;
b += x;
return *this;
}
DoubleArrayRAccIterator& 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& it) const {
a += it.a;
b += it.b;
return *this;
}
DoubleArrayRAccIterator DoubleArrayRAccIterator::operator-(const DoubleArrayRAccIterator& 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->() const {
// this MUST be wrong, only return value of array a?
doubleValue result;
result.a_val=a;
result.b_val=b;
return &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& it) const {
return a == it.a;
//compare indices or values here?
}
bool DoubleArrayRAccIterator::operator!=(const DoubleArrayRAccIterator& it) const {
return a != it.a;
//compare indices or values here?
}
bool DoubleArrayRAccIterator::operator<(const DoubleArrayRAccIterator& it) const {
//compare indices or values here?
}
bool DoubleArrayRAccIterator::operator>(const DoubleArrayRAccIterator& it) const {
//compare indices or values here?
}
bool DoubleArrayRAccIterator::operator<=(const DoubleArrayRAccIterator& it) const {
//compare indices or values here?
}
bool DoubleArrayRAccIterator::operator>=(const DoubleArrayRAccIterator& 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);
}
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 http://www.c-plusplus.de/forum/313698-full, what are they supposed to be?