Reputation: 113
I am trying to use the same variable for both iterator and reverse iterator. However, I cannot find the common base class to do so. I want to do something like this:
string a;
....
string::iterator i;
if (....)
i = a.begin();
else
i = a.rbegin(); //Error
....
Is there a good way of doing this?
Upvotes: 2
Views: 693
Reputation: 62636
There is no common type for std::string::iterator
and std::string::reverse_iterator
.
If you want to choose between them, you will need to have a different function for each type. The simplest way to do that is with a template
E.g.
template <typename Iterator>
void do_stuff(Iterator begin, Iterator end)
{
...
}
int main()
{
string a;
if (...)
do_stuff(a.begin(), a.end());
else
do_stuff(a.rbegin(), a.rend());
return 0;
}
Upvotes: 7