我从 C++ 食谱中复制了一些代码来实现跨步迭代器。迭代器似乎适用于其他stl
函数,例如copy
,但不适用于sort
. 我的猜测是,这与缺少一些操作员有关。这是我的跨步迭代器的头文件(来自 Oreilly C++ 食谱)
#define STRIDEITER_HPP
#include <iterator>
#include <cassert>
template<class Iter_T>
class stride_iter
{
public:
// public typedefs
typedef typename std::iterator_traits<Iter_T>::value_type value_type;
typedef typename std::iterator_traits<Iter_T>::reference reference;
typedef typename std::iterator_traits<Iter_T>::difference_type difference_type;
typedef typename std::iterator_traits<Iter_T>::pointer pointer;
typedef std::random_access_iterator_tag iterator_category;
typedef stride_iter self;
// constructors
stride_iter( ) : m(NULL), step(0) { };
stride_iter(const self& x) : m(x.m), step(x.step) { }
stride_iter(Iter_T x, difference_type n) : m(x), step(n) { }
// operators
self& operator++( ) { m += step; return *this; }
self operator++(int) { self tmp = *this; m += step; return tmp; }
self& operator+=(const difference_type x) { m += (x * step); return *this; }
self& operator--( ) { m -= step; return *this; }
self operator--(int) { self tmp = *this; m -= step; return tmp; }
self& operator-=(const difference_type x) { m -= x * step; return *this; }
reference operator[](const difference_type n) { return m[n * step]; }
reference operator*( ) { return *m; }
// friend operators
friend bool operator==(const self& x, const self& y) {
assert(x.step == y.step);
return x.m == y.m;
}
friend bool operator!=(const self& x, const self& y) {
assert(x.step == y.step);
return x.m != y.m;
}
friend bool operator<(const self& x, const self& y) {
assert(x.step == y.step);
return x.m < y.m;
}
friend difference_type operator-(const self& x, const self& y) {
assert(x.step == y.step);
return (x.m - y.m) / x.step;
}
friend self operator+(const self& x, difference_type y) {
assert(x.step == y.step);
return x += (y * x.step);
}
friend self operator+(difference_type x, const self& y) {
assert(x.step == y.step);
return y += x * x.step;
}
private:
Iter_T m;
difference_type step;
};
#endif
我打电话正在使用
#include "strideiter.hpp"
#include <algorithm>
#include <iterator>
#include <iostream>
using namespace std;
int main( ) {
int *a;
a =(int*) malloc(10*sizeof(int));
for(int i=0; i<10; i++)
{
a[i]=10-i;
}
int skip=2;
stride_iter<int*> first(a+2, skip);
stride_iter<int*> last(a + 2+8, skip);
sort(first,last);
}
我收到几个错误,第一个是:
strideiter.hpp(52): error: expression must have class type
assert(x.step == y.step);
我需要多个实现+=
吗?