- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
代码 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 << " ";
CODE A 工作正常但是为什么代码 B 通过错误:
DEV C++\vector_test.cpp 在 'rcit != std::vector<_Tp, _Alloc>::rend() 与 _Tp = int, _Alloc = std::allocator 中没有匹配 'operator!=' '.
这是我尝试编写的程序。
#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;
}
最佳答案
问题是 rend
方法有两种形式:
reverse_iterator rend();
const_reverse_iterator rend() const;
在进行比较时,似乎使用了第一个(尽管我不知道为什么),并且运算符 !=
用于比较“const”和“non-const”迭代器没有定义。但是,在分配给变量时,编译器可以推断出要调用的正确函数,并且一切正常。
关于c++ - 反向迭代器错误 : no match for 'operator!=' in 'rcit != std::vector<_Tp, _Alloc>::rend() with _Tp = int, _Alloc = std::allocator' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5907078/
代码 A: vector::const_reverse_iterator rcit; vector::const_reverse_iterator tit=v.rend(); for(rcit = v
问题是遍历一个段落,直到遇到一个空行。 这是给我标题中提到的错误的代码 #include #include #include int main() { using namespace s
我在 MS visual studio 2005 的 Windows 上运行 Openipmp 客户端。尽管根据文档,它仅在 visual studio 6 和 MS visual studio .N
不幸的是,我的任务是编译一个旧的 C++ DLL,以便它可以在 Win 7 64 位机器上运行。我的 C++ 经验为零。我已经解决了其他问题,但是这个问题难倒了我,我还没有在其他地方找到解决方案。以下
将 VC6 代码迁移到 VS2008 时出现以下错误。此代码在 VC6 中运行良好,但在 VC9 中会出现编译错误。我知道这是因为编译器的破坏性变化。问题是什么,我该如何解决? error C2440
我收到以下错误: error C2440: 'type cast' : cannot convert from 'std::_Vector_iterator' to 'DWORD' w
我遇到编译错误, required from 'std::pair, bool> std::_Rb_tree::_M_insert_unique(_Arg&&) [with _Arg = Soluti
我无法将元素插入 vector 。 void Addvec(const vector &v) { int num{}; cout>num; v.push_back(num);
我是一名优秀的程序员,十分优秀!