#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 is leagl, right? why const_iterator can not? I am puzzled. This program just want to recursive print a vector. I want to use reference for the iterator. Because I donot need to modify elements in vector, I used the const_iterator with cbegin() and cend() memeber function. I think bind a const var reference to a const var or literal value is right. But my code cannot pass compiling stage. If I use const_iterator not const_iterator& in function fomal args, my code could run well.
Here you go:
The thing is, you cannot bind a temporary (the iterators returned by begin() et al.) to a non-const reference. So either accept
vector<int>::const_iterator const&or take those iterators by value (which isn't that bad).