微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法将const_iterator绑定到const_iterator

如何解决无法将const_iterator绑定到const_iterator

#include <iostream>
#include <vector>

using namespace std;

void print(vector<int>::const_iterator &beg,vector<int>::const_iterator &end);

int main()
{
    vector<int> ivec = {1,2,3,4,5};
    print(ivec.cbegin(),ivec.cend());

    return 0;
}

void print(vector<int>::const_iterator &beg,vector<int>::const_iterator &end)
{
    if (beg == end)
    {
        cout << "over" << endl;
        return;
    }
    cout << *beg << endl;
    print(++beg,end);
}

ubuntu 20,g ++ 9.0

rec_print_vect.cpp: In function ‘int main()’:
rec_print_vect.cpp:11:19: error: cannot bind non-const lvalue reference of type ‘std::vector<int>::const_iterator&’ {aka ‘__gnu_cxx::__normal_iterator<const int*,std::vector<int> >&’} to an rvalue of type ‘std::vector<int>::const_iterator’ {aka ‘__gnu_cxx::__normal_iterator<const int*,std::vector<int> >’}
   11 |  print(ivec.cbegin(),ivec.cend());
      |        ~~~~~~~~~~~^~
rec_print_vect.cpp:6:41: note:   initializing argument 1 of ‘void print(std::vector<int>::const_iterator&,std::vector<int>::const_iterator&)’
    6 | void print(vector<int>::const_iterator &beg,vector<int>::const_iterator &end);
 

const int &i = 10是步兵,对吗?为什么const_iterator不能?我很困惑。该程序只想递归打印矢量。我想将引用用于迭代器。因为我不需要修改vector中的元素,所以我将const_iterator与cbegin()和cend()memeber函数一起使用。我认为将const var引用绑定到const var或文字值是正确的。但是我的代码无法通过编译阶段。如果在fomal args函数中使用const_iterator而不是const_iterator&,则我的代码可以正常运行。

解决方法

您在这里:

void print(vector<int>::const_iterator beg,vector<int>::const_iterator end);

问题是,您不能将临时(由begin()等返回的迭代器)绑定到非常量引用。因此,要么接受vector<int>::const_iterator const&,要么按值接受那些迭代器(这还不错)。

,

功能参数

void print(vector<int>::const_iterator &beg,vector<int>::const_iterator &end);

是非恒定引用。而此调用中的参数

print(ivec.cbegin(),ivec.cend());

是成员函数cbegincend返回的临时对象。您不得将非恒定左值引用与临时对象绑定。

声明类似的功能

void print(vector<int>::const_iterator beg,vector<int>::const_iterator end);

请注意,由于该语句,当为空向量调用该函数时,该函数可以调用未定义的行为

if (beg + 1 == end)

该函数可以更简单地定义。

void print(vector<int>::const_iterator beg,vector<int>::const_iterator end)
{ 
    if ( beg != end )
    {
        std::cout << *beg << '\n';
        print( ++beg,end );
    }
}

或者可以定义函数,如下面的演示程序中所示。

#include <iostream>
#include <vector>
#include <iterator>

std::ostream & print( std::vector<int>::const_iterator beg,std::vector<int>::const_iterator end,std::ostream &os = std::cout )
{
    return beg != end ? os << *beg << '\n',print( ++beg,end ) : os;
}

int main() 
{
    std::vector<int> ivec = {1,2,3,4,5};
    
    print( std::begin( ivec ),std::end( ivec ) ) << std::endl;
    
    return 0;
}

其输出为

1
2
3
4
5

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?