反向迭代器错误:与“operator!=”不匹配在 'rcit != std::vector<_tp , _alloc>::rend() 中, _Tp = int, _Alloc = std::allocator'

发布于 2024-11-05 21:02:36 字数 1970 浏览 0 评论 0原文

代码 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲喜皆因你 2024-11-12 21:02:36

问题是有两种形式的 rend 方法:

reverse_iterator rend();
const_reverse_iterator rend() const;

在进行比较时,似乎使用了第一种形式(尽管我不知道为什么),而运算符 != 未定义。但是,当分配给变量时,编译器可以推断出要调用的正确函数,并且一切正常。

The problem is there are two forms of the rend method:

reverse_iterator rend();
const_reverse_iterator rend() const;

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.

郁金香雨 2024-11-12 21:02:36

当我在 Xcode 4 或键盘中编译以下内容时,我没有收到任何错误或警告:

#include <iostream>
#include <vector>

int main () {
    using namespace std;
    vector< int > v;

    vector< int >::const_reverse_iterator rcit;
    for(rcit = v.rbegin(); rcit != v.rend(); ++rcit)
        cout << *rcit << " ";
}

您的程序与此有何显着差异吗?你使用什么编译器?奥格尼安的建议有效吗?如果将 != 替换为 == 会怎样? (我知道这会导致运行时错误,但我很好奇编译器对此的响应。)

I don't get any errors or warnings when I compile the following in Xcode 4 or codepad:

#include <iostream>
#include <vector>

int main () {
    using namespace std;
    vector< int > v;

    vector< int >::const_reverse_iterator rcit;
    for(rcit = v.rbegin(); rcit != v.rend(); ++rcit)
        cout << *rcit << " ";
}

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.)

沉睡月亮 2024-11-12 21:02:36

尝试像这样交换 != 的操作数:

for(rcit = v.rbegin(); v.rend() != rcit; ++rcit)

Try swapping the operands of != like this:

for(rcit = v.rbegin(); v.rend() != rcit; ++rcit)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文