Check if iterator precedes another

I would like to check if the iterator (or similar types) precedes the other. In this example, I would like to verify that I do not have an infinite loop. This means that "if I apply the ++ operator enough time to start, I will come to an end." Is this possible in C ++ 98? Maybe with a restriction on type T?

/**
 * \brief Loop
 * \attention T must implement operator++() and operator!=(const T&)
 * \param begin Begin of the loop
 * \param end End of the loop
 * \pre begin precedes end
 */
template <typename T>
void loop(const T& begin, const T& end)
{
  T run = begin;
  while(run != end)
  {
    /* do something with run */
    ++run;
  }
}

loop(0,10);

std::set<double> x;
x.insert(1.0);
x.insert(2.0);
x.insert(3.0);
loop(x.begin(), x.end());
+4
source share
1 answer

, , std::vector, std::basic_string, std::deque std::array, , , operator< ( ). , , .

+7

All Articles