I am currently in the process of learning C++ and encounter an issue while using
std::string::reverse_iterator
to reverse a string. I get nasty compiler errors when trying to run the function below. However, when I switch to using,
std::string::const_reverse_iterator
,the code compiles and runs successfully. Why is this the case, especially when the documentation for the language says that reverse iterators can be declared and used? What if I need to say, remove elements from a string while looping through it in reverse, and want to use a reverse iterator? A
const_reverse_iterator
surely would not suffice in this case. Any help would be much appreciated. :)
std::string reverse(const std::string &str)
{
std::string::reverse_iterator r_iter;
std::string result;
for (r_iter = str.rbegin(); r_iter < str.rend(); r_iter++) {
result += (*r_iter);
}
return result;
}
Some of these errors are:
/usr/include/c++/7/bits/stl_iterator.h: In instantiation of ‘std::reverse_iterator<_Iterator>::reverse_iterator(const std::reverse_iterator<_Iter>&) [with _Iter = __gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >; _Iterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >]’:
chap6.cpp:40:34: required from here
/usr/include/c++/7/bits/stl_iterator.h:148:22: error: no matching function for call to ‘__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >::__normal_iterator(std::reverse_iterator<__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> > >::iterator_type)’
: current(__x.base()) { }
and
/usr/include/c++/7/bits/stl_iterator.h:775:26: note: candidate expects 0 arguments, 1 provided
/usr/include/c++/7/bits/stl_iterator.h:760:11: note: candidate: constexpr __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >::__normal_iterator(const __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >&)
class __normal_iterator
^~~~~~~~~~~~~~~~~
/usr/include/c++/7/bits/stl_iterator.h:760:11: note: no known conversion for argument 1 from ‘std::reverse_iterator<__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> > >::iterator_type {aka __gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >}’ to ‘const __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >&’
You have a
const std::string
, which means that you can only doconst
things to it. There are two overloads ofstd::string::rbegin()
:The first is unavailable to you, but the second is not.
Note that you don't even need the loop, because you can construct a
std::string
from a pair of iterators, see overload (6)