gpt4 book ai didi

c++ - 反向迭代器错误 : no match for 'operator!=' in 'rcit != std::vector<_Tp, _Alloc>::rend() with _Tp = int, _Alloc = std::allocator'

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:12:49 25 4
gpt4 key购买 nike

代码 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com