反向迭代器错误:与“operator!=”不匹配在 'rcit != std::vector<_tp , _alloc>::rend() 中, _Tp = int, _Alloc = std::allocator'
代码 A:
vector< int >::const_reverse_iterator rcit;
vector< int >::const_reverse_iterator tit=v.rend();
for(rcit = v.rbegin(); rcit != tit; ++rcit)
cout << *rcit << " ";
代码 B:
vector< int >::const_reverse_iterator rcit;
for(rcit = v.rbegin(); rcit != v.rend(); ++rcit)
cout << *rcit << " ";
代码 A 工作正常,但是 为什么代码 B 会出现错误:
DEV C++\vector_test.cpp 与 'rcit != std::vector<_Tp, _Alloc>::rend() 中的 'operator!=' 不匹配,其中 _Tp = int, _Alloc = std::分配器'。
这是我试图编写的程序。
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using namespace std;
#include <vector>
using std::vector;
template< typename T > void printVector( const vector< T > &v);
template< typename T >
void printVector( const vector< T > &v)
{
typename vector< T >::const_iterator cit;
for(cit = v.begin(); cit != v.end(); ++cit)
cout << *cit << " ";
}
int main()
{
int number;
vector< int > v;
cout << "Initial size of the vector : " << v.size()
<< " and capacity : " << v.capacity() << endl;
for(int i=0; i < 3; i++)
{
cout << "Enter number : ";
cin >> number;
v.push_back(number);
}
cout << "Now size of the vector : " << v.size()
<< "and capacity : " << v.capacity() << endl;
cout << "output vector using iterator notation " << endl;
printVector(v);
cout << "Reverse of output ";
vector< int >::const_reverse_iterator rcit;
for(rcit = v.rbegin(); v.rend() != rcit ; ++rcit)
cout << *rcit << " ";
cin.ignore(numeric_limits< streamsize >::max(), '\n');
cin.get();
return 0;
}
CODE A:
vector< int >::const_reverse_iterator rcit;
vector< int >::const_reverse_iterator tit=v.rend();
for(rcit = v.rbegin(); rcit != tit; ++rcit)
cout << *rcit << " ";
CODE B:
vector< int >::const_reverse_iterator rcit;
for(rcit = v.rbegin(); rcit != v.rend(); ++rcit)
cout << *rcit << " ";
CODE A works fine but
why does CODE B throughs error :
DEV C++\vector_test.cpp no match for 'operator!=' in 'rcit != std::vector<_Tp, _Alloc>::rend() with _Tp = int, _Alloc = std::allocator'.
This is the program I was trying to write.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using namespace std;
#include <vector>
using std::vector;
template< typename T > void printVector( const vector< T > &v);
template< typename T >
void printVector( const vector< T > &v)
{
typename vector< T >::const_iterator cit;
for(cit = v.begin(); cit != v.end(); ++cit)
cout << *cit << " ";
}
int main()
{
int number;
vector< int > v;
cout << "Initial size of the vector : " << v.size()
<< " and capacity : " << v.capacity() << endl;
for(int i=0; i < 3; i++)
{
cout << "Enter number : ";
cin >> number;
v.push_back(number);
}
cout << "Now size of the vector : " << v.size()
<< "and capacity : " << v.capacity() << endl;
cout << "output vector using iterator notation " << endl;
printVector(v);
cout << "Reverse of output ";
vector< int >::const_reverse_iterator rcit;
for(rcit = v.rbegin(); v.rend() != rcit ; ++rcit)
cout << *rcit << " ";
cin.ignore(numeric_limits< streamsize >::max(), '\n');
cin.get();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是有两种形式的
rend
方法:在进行比较时,似乎使用了第一种形式(尽管我不知道为什么),而运算符
!= 未定义。但是,当分配给变量时,编译器可以推断出要调用的正确函数,并且一切正常。
The problem is there are two forms of the
rend
method:When doing the comparison it seems the first is being used (though I don't know why), and the operator
!=
for comparing a 'const' and a 'non-const' iterator is not defined. When assigning to the variable though, the compiler can deduce the correct function to call and it all works correctly.当我在 Xcode 4 或键盘中编译以下内容时,我没有收到任何错误或警告:
您的程序与此有何显着差异吗?你使用什么编译器?奥格尼安的建议有效吗?如果将
!=
替换为==
会怎样? (我知道这会导致运行时错误,但我很好奇编译器对此的响应。)I don't get any errors or warnings when I compile the following in Xcode 4 or codepad:
Does your program differ from this in any significant way? What compiler are you using? Does ognian's suggestion work? And what if you replace the
!=
with an==
? (I know it would cause a run-time error but I curious about the compiler's response to this.)尝试像这样交换 != 的操作数:
Try swapping the operands of != like this: