[英]C++ How to make converting from iterator to const_iterator for own iterator class?
我正在為它編寫一個類似向量的 STL 和一個迭代器 class 。 而今天在寫帶有const_iterator的erase func時,結果發現沒有從iterator到const_iterator的對話。我試圖找到一些信息,但我仍然不知道如何制作。 但是 STL 向量以某種方式實現了它。 我的迭代器 class:
template<typename VectorDataType>
class Iterator{
public:
using value_type = VectorDataType;
using difference_type = int;
using pointer = VectorDataType*;
using reference = VectorDataType&;
using iterator_category = std::random_access_iterator_tag;
public:
explicit Iterator(VectorDataType* ptr = nullptr)
: ptr(ptr){}
Iterator(const Iterator<VectorDataType>& iter)
: ptr(iter.ptr){}
~Iterator() {ptr = nullptr;}
template<template<typename Y> class Alloc>
operator typename Vector<VectorDataType, Alloc>::const_iterator (){
typename Vector<VectorDataType, Alloc>::const_iterator citer(*this);
return citer;
}
Iterator& operator=(VectorDataType* rhs) {this->ptr = rhs; return *this;}
Iterator& operator=(const Iterator& rhs) {this->ptr = rhs.ptr; return *this;}
operator bool() const{
bool result = (this->ptr) ? true : false;
return result;
}
bool operator==(const Iterator<VectorDataType>& iter) const{
return this->ptr == iter.ptr;
}
bool operator!=(const Iterator<VectorDataType>&iter) const{
return this->ptr != iter.ptr;
}
bool operator<(const Iterator<VectorDataType>& iter)const {
return this->ptr < iter.ptr;
}
bool operator>(const Iterator<VectorDataType>& iter)const {
return this->ptr > iter.ptr;
}
bool operator<=(const Iterator<VectorDataType>& iter)const {
return this->ptr <= iter.ptr;
}
bool operator>=(const Iterator<VectorDataType>& iter)const {
return this->ptr >= iter.ptr;
}
Iterator<VectorDataType>& operator+=(const difference_type& offset){
this->ptr += offset;
return *this;
}
Iterator<VectorDataType>& operator-=(const difference_type& offset){
this->ptr -= offset;
return *this;
}
Iterator<VectorDataType>& operator++(){
this->ptr++;
return *this;
}
Iterator<VectorDataType>& operator--(){
this->ptr--;
return *this;
}
Iterator<VectorDataType> operator++(int){
Iterator<VectorDataType> temp(*this);
this->ptr++;
return temp;
}
Iterator<VectorDataType> operator--(int){
Iterator<VectorDataType> temp(*this);
this->ptr--;
return temp;
}
Iterator<VectorDataType> operator+(const difference_type&offset) const{
VectorDataType* newPtr = ptr;
newPtr += offset;
return Iterator{newPtr};
}
Iterator<VectorDataType> operator-(const difference_type&offset) const{
VectorDataType* newPtr = ptr;
newPtr -= offset;
return Iterator{newPtr};
}
difference_type operator-(const Iterator<VectorDataType>& iter)const{
return std::distance(iter.ptr, this->ptr);
}
VectorDataType& operator*(){return *ptr;}
const VectorDataType& operator*()const{return *ptr;}
VectorDataType* operator->(){return ptr;}
VectorDataType& operator[](int offset){
return ptr[offset];
}
private:
VectorDataType* ptr;
};
主要代碼:
auto list = {1, 2, 3, 4, 5, 6, 7, 8, 9};
Vector<int> vec(list);
for(auto&i: vec)
std::cout << i << "\t";
auto iter = vec.begin();
iter++;
iter++;
iter++;
auto pos = vec.erase(iter);
for(auto&i: vec)
std::cout << i << "\t";
擦除定義:
iterator erase(const_iterator pos);
控制台日志:
g++ -c -pipe -g -std=gnu++1z -Wall -Wextra -fPIC -I../MyVector -I. -I../../Qt/5.14.1/gcc_64/mkspecs/linux-g++ -o main.o ../MyVector/main.cpp
../MyVector/main.cpp: In function ‘int main()’:
../MyVector/main.cpp:49:30: error: no matching function for call to ‘Vector<int>::erase(Iterator<int>&)’
auto pos = vec.erase(iter);
^
In file included from ../MyVector/main.cpp:2:0:
../MyVector/MyVector.h:601:21: note: candidate: Vector<T, Allocator>::iterator Vector<T, Allocator>::erase(Vector<T, Allocator>::const_iterator) [with T = int; Allocator = std::allocator; Vector<T, Allocator>::iterator = Iterator<int>; Vector<T, Allocator>::const_iterator = Iterator<const int>]
iterator erase(const_iterator pos){
^~~~~
../MyVector/MyVector.h:601:21: note: no known conversion for argument 1 from ‘Iterator<int>’ to ‘Vector<int>::const_iterator {aka Iterator<const int>}’
../MyVector/MyVector.h:618:21: note: candidate: Vector<T, Allocator>::iterator Vector<T, Allocator>::erase(Vector<T, Allocator>::const_iterator, Vector<T, Allocator>::const_iterator) [with T = int; Allocator = std::allocator; Vector<T, Allocator>::iterator = Iterator<int>; Vector<T, Allocator>::const_iterator = Iterator<const int>]
如果您想查看所有代碼,這里有一個 github 鏈接: https://github.com/RRRadicalEdward/Vector/blob/master/MyVector.h
與所有用戶定義的轉換方式相同:
任何一個
您的隱式轉換運算符的問題在於它將模板模板參數Alloc
置於非推導上下文中,即留給 scope 運算符:
template <template <typename Y> class Alloc>
operator typename Vector<VectorDataType, Alloc>::const_iterator() {
// non-deduced ~~~~^
typename Vector<VectorDataType, Alloc>::const_iterator citer(*this);
return citer;
}
因此編譯器不能應用這種轉換。 嘗試將其更改為:
operator Iterator<const VectorDataType>() const {
Iterator<const VectorDataType> citer(ptr);
return citer;
}
另請注意,構造函數調用參數是一個指針,而不是迭代器 ( *this
) 本身。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.