3

我有一个向量

struct Data {
    size_t iLo;
    size_t iHi;
};

我想对 和 的值进行iLo单独iHi排序,即如果我对iLo成员进行排序,则不会触及“iHi”成员。iLos 升序排序,iHis 降序排序。例如:

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

首先对 s 进行升序排序iLo会给我

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

然后对 s 进行降序排序iHi将导致

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

我想这样做的原因是我正在处理大量数据并且可能没有足够的 RAM 来将原始数组数据拆分为两个新数据。我想先在原地试一试。

我不能使用 Boost,只能使用 c++03。

4

3 回答 3

3

您必须为您的 编写一个随机访问迭代器vector<Data>它返回对 iLo(或 iHi,resp.)的引用。

完整示例:

#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;
}

现场运行:http: //ideone.com/gr2zSj

于 2013-03-10T04:06:38.163 回答
2

试图哄骗标准库以这种方式进行排序将是一场噩梦。

您最好的选择是复制一个简单的快速排序例程,并根据您的特定目的进行调整。它应该只是关于一页代码。

于 2013-03-10T04:07:36.810 回答
-2
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;
    }
};

按 iLo 排序:

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

按 iHi 排序:

Data d[3] = {{3, 4}, {1, 2}, {5, 6}};
sort(&d[0], &d[3], HiComparator());
于 2013-03-10T03:17:36.143 回答