自定义迭代器用于find泛型算法
我自定义了一个迭代器,并应用于自定义的数据结构上,像这样的:
#include <iterator>
#include <algorithm>
#include <deque>
///============================自定义的迭代器==================================//
template<class T, class CategoryType = std::random_access_iterator_tag>
class CMyVectorIterator : public std::iterator<CategoryType, T>
{
public:
CMyVectorIterator() : m_PointerValue(0){}
CMyVectorIterator(const pointer& vPointerValue) : m_PointerValue(vPointerValue){}
reference operator*(){return *m_PointerValue;}
pointer operator-> (){return m_PointerValue;}
CMyVectorIterator& operator++ (){m_PointerValue += 1;return *this;}
CMyVectorIterator& operator-- (){m_PointerValue -= 1; return *this;}
public:
typedef CMyVectorIterator iterator;
private:
pointer m_PointerValue;
};
//=============================跟迭代器对应的vector的数据结构======================//
template<class T, size_t BufferSize>
class CMyVector
{
public:
//CMyVector(){}
CMyVector() : m_First(m_DataSet), m_Last(m_DataSet + BufferSize), m_HasBeen(0){}
void push_back(T vValue)
{
if(m_HasBeen < BufferSize && m_HasBeen != BufferSize - 1)
{m_DataSet[m_HasBeen] = vValue; ++m_HasBeen;return;}
return;
}
CMyVectorIterator<T> insertAt(const size_t vWhere, T vValue);
CMyVectorIterator<T> eraseAt(const size_t vWhere);
CMyVectorIterator<T> begin(){return m_First;}
CMyVectorIterator<T> end(){return m_Last;}
typedef CMyVectorIterator<T> iterator;
private:
T m_DataSet[BufferSize];
size_t m_HasBeen;
CMyVectorIterator<T> m_First;
CMyVectorIterator<T> m_Last;
};
//============================最后是像这样使用的====================//
#include "MyIterator.h"
void main()
{
CMyVector<int, 10> MyVector;
MyVector.push_back(0);
MyVector.push_back(1);
MyVector.push_back(2);
MyVector.push_back(3);
MyVector.push_back(4);
MyVector.push_back(5);
MyVector.push_back(6);
CMyVector<int, 10>::iterator Begin = MyVector.begin();
CMyVector<int, 10>::iterator End = MyVector.end();
CMyVector<int, 10>::iterator Iter = std::find(Begin, End, 3);
}
但是的编译错误居然跟deque有关系,搞不懂;还望大神们指点一番;
//编译错误如下:
1>------ Build started: Project: MyIterator, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>d:\program files\microsoft visual studio 9.0\vc\include\algorithm(39) : error C2784: 'bool std::operator !=(const std::deque<_Ty,_Alloc> &,const std::deque<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::deque<_Ty,_Alloc> &' from 'CMyVectorIterator<T>'
1> with
1> [
1> T=int
1> ]
1> d:\program files\microsoft visual studio 9.0\vc\include\deque(1341) : see declaration of 'std::operator !='
1> d:\program files\microsoft visual studio 9.0\vc\include\algorithm(74) : see reference to function template instantiation '_InIt std::_Find<CMyVectorIterator<T>,_Ty>(_InIt,_InIt,const _Ty &)' being compiled
1> with
1> [
1> _InIt=CMyVectorIterator<int>,
1> T=int,
1> _Ty=int
1> ]
1> e:\学习资料1\c++\mystl\stl\myiterator\main.cpp(15) : see reference to function template instantiation '_InIt std::find<CMyVectorIterator<T>,int>(_InIt,_InIt,const _Ty &)' being compiled
1> with
1> [
1> _InIt=CMyVectorIterator<int>,
1> T=int,
1> _Ty=int
1> ]
1>d:\program files\microsoft visual studio 9.0\vc\include\algorithm(39) : error C2784: 'bool std::operator !=(const std::deque<_Ty,_Alloc> &,const std::deque<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::deque<_Ty,_Alloc> &' from 'CMyVectorIterator<T>'
1> with
1> [
1> T=int
1> ]
1> d:\program files\microsoft visual studio 9.0\vc\include\deque(1341) : see declaration of 'std::operator !='
1>d:\program files\microsoft visual studio 9.0\vc\include\algorithm(39) : error C2784: 'bool std::operator !=(const std::deque<_Ty,_Alloc> &,const std::deque<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::deque<_Ty,_Alloc> &' from 'CMyVectorIterator<T>'
1> with
1> [
1> T=int
1> ]
1> d:\program files\microsoft visual studio 9.0\vc\include\deque(1341) : see declaration of 'std::operator !='
1>d:\program files\microsoft visual studio 9.0\vc\include\algorithm(39) : error C2784: 'bool std::operator !=(const std::allocator<_Ty> &,const std::allocator<_Other> &) throw()' : could not deduce template argument for 'const std::allocator<_Ty> &' from 'CMyVectorIterator<T>'
1> with
1> [
1> T=int
1> ]
1> d:\program files\microsoft visual studio 9.0\vc\include\xmemory(180) : see declaration of 'std::operator !='
迭代器 泛型
[解决办法]
需要提供 CMyVectorIterator 之间的比较运行符,比如 <, ==, != 之类的。
m_Last 的位置也不正确,应该指向末尾元素的下一个位置,而不是存储空间的下一个位置。
[解决办法]
+1
[解决办法]
几个自定义运算符
[解决办法]
楼主还是多啃啃《stl源码剖析》吧