Question Details

No question body available.

Tags

c++ for-loop c++11 iterator

Answers (1)

July 12, 2026 Score: 3 Rep: 38,064 Quality: Medium Completeness: 80%

How do I declare them and initialize depending on n?

The problem is that begin() and end() return a std::vector::iterator, whereas rbegin() and rend() return a std::vector::reverseiterator. These are different types, and your itb,ite cannot be both.

One solution is to use an intermediate function template, where the type of iterator is the template parameter. Then you can pass different types of iterators as arguments.

This is demonstrated below:

#include 
#include 

template void my
loop(IterT const& from, IterT const& to) { for (IterT it = from; it != to; it++) { std::cout