James
James

Reputation: 9278

C+ sorting individual members of a vector element

I have a vector of

struct Data {
    size_t iLo;
    size_t iHi;
};

I want to sort the values of iLo and iHi individually, i.e. if I sort the iLo members the 'iHi' members aren't touched. iLos are sorted ascending and iHis are sorted descending. For example:

{{1, 3}, {4, 66}, {0, 0}, {0, 1}}; 

First sorting ascending the iLos would give me

{{0, 3}, {0, 66}, {1, 0}, {4, 1}};

Then sorting descending the iHis would result in

{{0, 66}, {0, 3}, {1, 1}, {4, 0}};

The reason I want to do this is that I'm dealing with a huge amount of data and may not have enough RAM to to split the original array data into two new ones. I'd like to try it in-place first.

I cannot use Boost and only up to c++03.

Upvotes: 3

Views: 140

Answers (3)

Kijewski
Kijewski

Reputation: 26043

You have to write an random access iterator to your vector<Data>, that returns a reference to iLo (or iHi, resp.).

Complete example:

#include <vector>
#include <algorithm>
#include <iterator>
#include <functional>
#include <iostream>

using namespace std;

struct Data {
    Data(size_t l, size_t h) : iLo(l), iHi(h) {}

    size_t iLo;
    size_t iHi;
};

// `MyIter` is the base for your iterators that return i->iLo / i->iHi.
template <class Impl> // Impl is the actual iterator type.
struct MyIter : public iterator<random_access_iterator_tag, size_t>
{
    typedef vector<Data>::iterator Base;
    Base base;

    MyIter(Base i) : base(i) {}

    // These are common operators that you have to define in GNU's std::sort.
    // The standard actually requires more operators, see
    // http://en.cppreference.com/w/cpp/concept/RandomAccessIterator

    bool operator !=(const Impl &rhs) const {
        return base != rhs.base;
    }
    bool operator ==(const Impl &rhs) const {
        return base == rhs.base;
    }
    bool operator <(const Impl &rhs) const {
        return base < rhs.base;
    }
    bool operator >(const Impl &rhs) const {
        return base > rhs.base;
    }
    bool operator >=(const Impl &rhs) const {
        return base >= rhs.base;
    }
    bool operator <=(const Impl &rhs) const {
        return base <= rhs.base;
    }
    ptrdiff_t operator -(const Impl &rhs) const {
        return base - rhs.base;
    }
    Impl operator +(ptrdiff_t i) const {
        return base + i;
    }
    Impl operator -(ptrdiff_t i) const {
        return base - i;
    }
    Impl &operator ++() {
        ++base;
        return static_cast<Impl&>(*this);
    }
    Impl &operator --() {
        --base;
        return static_cast<Impl&>(*this);
    }
    Impl &operator +=(size_t n) {
        base += n;
        return static_cast<Impl&>(*this);
    }
    Impl &operator -=(size_t n) {
        base -= n;
        return static_cast<Impl&>(*this);
    }
};

struct MyLoIter : public MyIter<MyLoIter>
{
    MyLoIter(Base i) : MyIter(i) {}

    size_t &operator [](int i) {
        return base[i].iLo;
    }
    size_t &operator *() {
        return base->iLo;
    }
};

struct MyHiIter : public MyIter<MyHiIter>
{
    MyHiIter(Base i) : MyIter(i) {}

    size_t &operator [](int i) {
        return base[i].iHi;
    }
    size_t &operator *() {
        return base->iHi;
    }
};

int main() {
    // I like C++11 a lot better ...
    vector<Data> data;
    data.push_back(Data(1, 3));
    data.push_back(Data(4, 66));
    data.push_back(Data(0, 0));
    data.push_back(Data(0, 1));

    // This is the actual sorting, first the iLo part, then the iHi part.
    // std::less and std::greater are used to sort descending and ascending-
    sort(MyLoIter(data.begin()), MyLoIter(data.end()), less<size_t>());
    sort(MyHiIter(data.begin()), MyHiIter(data.end()), greater<size_t>());

    // Now the test if it worked:
    for (vector<Data>::iterator i = data.begin(); i != data.end(); ++i) {
        cout << i->iLo << "\t" << i->iHi << endl;
    }
    return 0;
}

Live run: http://ideone.com/gr2zSj

Upvotes: 3

StilesCrisis
StilesCrisis

Reputation: 16300

Trying to coax the standard library into sorting this way will be a nightmare.

Your best bet is to copy a simple quick-sort routine and adapt it to your specific purpose. It should only be about a page of code.

Upvotes: 2

user1610015
user1610015

Reputation: 6678

struct LoComparator
{
    bool operator()(const Data& d1, const Data& d2)
    {
        return d1.iLo < d2.iLo;
    }
};

struct HiComparator
{
    bool operator()(const Data& d1, const Data& d2)
    {
        return d1.iHi > d2.iHi;
    }
};

To sort by the iLo's:

Data d[3] = {{3, 4}, {1, 2}, {5, 6}};
sort(&d[0], &d[3], LoComparator());

To sort by the iHi's:

Data d[3] = {{3, 4}, {1, 2}, {5, 6}};
sort(&d[0], &d[3], HiComparator());

Upvotes: -2

Related Questions