在 C++ 中实现随机访问迭代器有什么问题?
What's wrong with this implementation of random access iterator in C++?
我已经尝试为名为 MyIntArray 的 class 实现我自己的随机访问迭代器。我已经编写了随机访问迭代器概念所需的所有函数。
我想知道在 C++ 中实现随机访问迭代器有什么问题?与从
更改时一样
using iterator_category = random_access_iterator_tag;
给这个
using iterator_category = bidirectional_iterator_tag;
排序正常。我已经尝试了其他可以正常工作的迭代器类型。但是当涉及到随机访问时,排序不起作用并且仍然无法弄清楚原因。
#include <iostream>
#include <algorithm>
#include <initializer_list>
using namespace std;
class MyIntArray{
private:
int * arr;
const int size;
public:
class MIAIterator{
private:
int index;
int * start;
public:
using iterator_category = random_access_iterator_tag;
using value_type = int;
using difference_type = int;
using pointer = int*;
using reference = int&;
MIAIterator(int* s,int i=0):index(i){start=s;};
MIAIterator(const MIAIterator& it):index(it.index),start(it.start){};
MIAIterator& operator=(const MIAIterator& it);
bool operator==(const MIAIterator & it){return this->index==it.index;}
bool operator!=(const MIAIterator & it){return this->index!=it.index;}
bool operator <(const MIAIterator & it){return this->index<it.index;}
bool operator <=(const MIAIterator & it){return this->index<=it.index;}
bool operator >(const MIAIterator & it){return this->index>it.index;}
bool operator >=(const MIAIterator & it){return this->index>=it.index;}
MIAIterator& operator++();
MIAIterator operator++(int);
MIAIterator& operator--();
MIAIterator operator--(int);
MIAIterator operator+(int n);
MIAIterator operator-(int n);
int operator-(const MIAIterator & it_end){return it_end.index-this->index;}
int& operator[](int i){return *(start+i);}
int& operator*(){return *(start+index);}
friend MIAIterator operator+(int n,const MIAIterator& mia);
};
friend class MIAIterator;
MyIntArray(int s,int n);
MyIntArray(initializer_list<int> il);
~MyIntArray();
void print();
MIAIterator begin(){return MIAIterator(&arr[0],0);}
MIAIterator end(){return MIAIterator(&arr[0],size);};
};
int main() {
MyIntArray mia={4,5,8,-1,3,-8,-16,2};
mia.print();
std::sort(mia.begin(),mia.end());
mia.print();
for(int a:mia){
std::cout<<a<<std::endl;
}
}
//Friend Function
MyIntArray::MIAIterator operator+(int n,const MyIntArray::MIAIterator& mia){
return MyIntArray::MIAIterator(mia.start,mia.index+n);
}
//MyIntArray Class
MyIntArray::MyIntArray(int s=10,int n=0):size(s){
arr=new int [size+1];
for(int i=0;i<size;i++){
arr[i]=n;
}
}
MyIntArray::MyIntArray(initializer_list<int> il):size(il.size()){
arr=new int [size+1];
int i=0;
for(auto it= il.begin();it!=il.end();it++,i++){
arr[i]=(*it);
}
}
MyIntArray::~MyIntArray(){
delete [] arr;
}
void MyIntArray::print(){
for(int i=0;i<size;i++){
std::cout<<arr[i]<<" ";
}
std::cout<<std::endl;
}
//The MIAIterator Class
MyIntArray::MIAIterator& MyIntArray::MIAIterator::operator=(const MIAIterator& it){
if(this==&it)return (*this);
this->index=it.index;
this->start=it.start;
return (*this);
}
MyIntArray::MIAIterator& MyIntArray::MIAIterator::operator++(){
this->index+=1;
return (*this);
}
MyIntArray::MIAIterator MyIntArray::MIAIterator::operator++(int){
this->index+=1;
return MIAIterator(this->start,this->index-1);
}
MyIntArray::MIAIterator& MyIntArray::MIAIterator::operator--(){
this->index-=1;
return (*this);
}
MyIntArray::MIAIterator MyIntArray::MIAIterator::operator--(int){
this->index-=1;
return MIAIterator(this->start,this->index+1);
}
MyIntArray::MIAIterator MyIntArray::MIAIterator::operator+(int n){
return MIAIterator(this->start,this->index+n);
}
MyIntArray::MIAIterator MyIntArray::MIAIterator::operator-(int n){
return MIAIterator(this->start,this->index-n);
}
你的问题就在这里
int operator-(const MIAIterator & it_end){return this->index - it_end.index;}
应该是相反的
int operator-(const MIAIterator & it_end){return it_end.index - this->index;}
int operator-(const MIAIterator & it_end){return this->index - it_end.index;}
这是正确的方法
我已经尝试为名为 MyIntArray 的 class 实现我自己的随机访问迭代器。我已经编写了随机访问迭代器概念所需的所有函数。 我想知道在 C++ 中实现随机访问迭代器有什么问题?与从
更改时一样using iterator_category = random_access_iterator_tag;
给这个
using iterator_category = bidirectional_iterator_tag;
排序正常。我已经尝试了其他可以正常工作的迭代器类型。但是当涉及到随机访问时,排序不起作用并且仍然无法弄清楚原因。
#include <iostream>
#include <algorithm>
#include <initializer_list>
using namespace std;
class MyIntArray{
private:
int * arr;
const int size;
public:
class MIAIterator{
private:
int index;
int * start;
public:
using iterator_category = random_access_iterator_tag;
using value_type = int;
using difference_type = int;
using pointer = int*;
using reference = int&;
MIAIterator(int* s,int i=0):index(i){start=s;};
MIAIterator(const MIAIterator& it):index(it.index),start(it.start){};
MIAIterator& operator=(const MIAIterator& it);
bool operator==(const MIAIterator & it){return this->index==it.index;}
bool operator!=(const MIAIterator & it){return this->index!=it.index;}
bool operator <(const MIAIterator & it){return this->index<it.index;}
bool operator <=(const MIAIterator & it){return this->index<=it.index;}
bool operator >(const MIAIterator & it){return this->index>it.index;}
bool operator >=(const MIAIterator & it){return this->index>=it.index;}
MIAIterator& operator++();
MIAIterator operator++(int);
MIAIterator& operator--();
MIAIterator operator--(int);
MIAIterator operator+(int n);
MIAIterator operator-(int n);
int operator-(const MIAIterator & it_end){return it_end.index-this->index;}
int& operator[](int i){return *(start+i);}
int& operator*(){return *(start+index);}
friend MIAIterator operator+(int n,const MIAIterator& mia);
};
friend class MIAIterator;
MyIntArray(int s,int n);
MyIntArray(initializer_list<int> il);
~MyIntArray();
void print();
MIAIterator begin(){return MIAIterator(&arr[0],0);}
MIAIterator end(){return MIAIterator(&arr[0],size);};
};
int main() {
MyIntArray mia={4,5,8,-1,3,-8,-16,2};
mia.print();
std::sort(mia.begin(),mia.end());
mia.print();
for(int a:mia){
std::cout<<a<<std::endl;
}
}
//Friend Function
MyIntArray::MIAIterator operator+(int n,const MyIntArray::MIAIterator& mia){
return MyIntArray::MIAIterator(mia.start,mia.index+n);
}
//MyIntArray Class
MyIntArray::MyIntArray(int s=10,int n=0):size(s){
arr=new int [size+1];
for(int i=0;i<size;i++){
arr[i]=n;
}
}
MyIntArray::MyIntArray(initializer_list<int> il):size(il.size()){
arr=new int [size+1];
int i=0;
for(auto it= il.begin();it!=il.end();it++,i++){
arr[i]=(*it);
}
}
MyIntArray::~MyIntArray(){
delete [] arr;
}
void MyIntArray::print(){
for(int i=0;i<size;i++){
std::cout<<arr[i]<<" ";
}
std::cout<<std::endl;
}
//The MIAIterator Class
MyIntArray::MIAIterator& MyIntArray::MIAIterator::operator=(const MIAIterator& it){
if(this==&it)return (*this);
this->index=it.index;
this->start=it.start;
return (*this);
}
MyIntArray::MIAIterator& MyIntArray::MIAIterator::operator++(){
this->index+=1;
return (*this);
}
MyIntArray::MIAIterator MyIntArray::MIAIterator::operator++(int){
this->index+=1;
return MIAIterator(this->start,this->index-1);
}
MyIntArray::MIAIterator& MyIntArray::MIAIterator::operator--(){
this->index-=1;
return (*this);
}
MyIntArray::MIAIterator MyIntArray::MIAIterator::operator--(int){
this->index-=1;
return MIAIterator(this->start,this->index+1);
}
MyIntArray::MIAIterator MyIntArray::MIAIterator::operator+(int n){
return MIAIterator(this->start,this->index+n);
}
MyIntArray::MIAIterator MyIntArray::MIAIterator::operator-(int n){
return MIAIterator(this->start,this->index-n);
}
你的问题就在这里
int operator-(const MIAIterator & it_end){return this->index - it_end.index;}
应该是相反的
int operator-(const MIAIterator & it_end){return it_end.index - this->index;}
int operator-(const MIAIterator & it_end){return this->index - it_end.index;}
这是正确的方法