我有
的向量struct Data {
size_t iLo;
size_t iHi;
};
我想单独对iLo
和iHi
的值进行排序,即如果我对iLo
成员进行排序
“iHi”成员没有被触及。 iLo
按升序排序,iHi
按降序排序。例如:
{{1, 3}, {4, 66}, {0, 0}, {0, 1}};
首先按iLo
升序排序会给我
{{0, 3}, {0, 66}, {1, 0}, {4, 1}};
然后按iHi
降序排序将导致
{{0, 66}, {0, 3}, {1, 1}, {4, 0}};
我想这样做的原因是我正在处理大量数据,并且可能没有足够的RAM来将原始数组数据拆分为两个新数据。我想尝试一下 就地。
我不能使用Boost,只能使用c ++ 03。
答案 0 :(得分:3)
您必须向vector<Data>
写一个random access iterator,它会返回对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;
}
答案 1 :(得分:2)
试图哄骗标准库以这种方式排序将是一场噩梦。
您最好的选择是复制一个简单的快速排序程序,并根据您的具体目的进行调整。它应该只是一页代码。
答案 2 :(得分:-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());