为什么不同的vector,他们的迭代器不能相互比较呢?
据我所知,vector的迭代器类型就是指针啊,按道理两个指针是可以相互比较大小的啊(虽然有时候没任何意义),但是怎么两个不同的vector(类型一样),他们的迭代器一比较就出错呢?比如下面的代码
#include<iostream>vector
#include<vector>
using namespace std;
int main()
{
int a[2]={1,2};
vector<int>::iterator it1,it2;
vector<int> ivec1(a,a+2);
vector<int> ivec2(a,a+2);
it1=ivec1.begin();
it2=ivec2.begin();
bool b=it1<it2;
system("pause");
return 0;
}
// vector
bool operator<(const _Myt& _Right) const
{// test if this < _Right
#if _HAS_ITERATOR_DEBUGGING
_Compat(_Right);
#else
_SCL_SECURE_VALIDATE(this->_Has_container() && this->_Same_container(_Right));
#endif /* _HAS_ITERATOR_DEBUGGING */
return (_Myptr < _Right._Myptr);
}
#if _HAS_ITERATOR_DEBUGGING
void _Compat(const _Myt& _Right) const
{// test for compatible iterator pair
if (this->_Mycont == 0
[解决办法]
this->_Mycont != _Right._Mycont)
////判断两个向量的型类是否一致
{
_DEBUG_ERROR("vector iterators incompatible");
_SCL_SECURE_INVALID_ARGUMENT;
}
}
#endif /* _HAS_ITERATOR_DEBUGGING */
Supported only for vector and deque.