Looping on a closed range

不想你离开。 提交于 2019-12-23 13:58:12

问题


How would you fix this code?

template <typename T> void closed_range(T begin, T end)
{
    for (T i = begin; i <= end; ++i) {
        // do something
    }
}
  • T is constrained to be an integer type, can be the wider of such types and can be signed or unsigned

  • begin can be numeric_limits<T>::min()

  • end can be numeric_limits<T>::max() (in which case ++i will overflow in the above code)

I've several ways, but none I really like.


回答1:


Maybe,

template <typename T> void closed_range(T begin, const T end)
    if (begin <= end) {
        do {
            // do something
        } while (begin != end && (++begin, true));
    }
}

Curses, my first attempt was wrong, and the fix above isn't as pretty as I'd hoped. How about:

template <typename T> bool advance(T &value) { ++value; return true; }

template <typename T> void closed_range(T first, const T last)
    if (first <= last) {
        do {
            // do something
        } while (first != last && advance(first));
    }
}

There's no ambiguity with std::advance even if T isn't an integer type, since std::advance takes 2 parameters. So the template would also work with for instance a random-access iterator, if for some reason you wanted a closed range of those.

Or how about a bit of set theory? Obviously this is massive overkill if you're only writing one loop over a closed range, but if it's something that you want to do a lot, then it makes the loop code about right. Not sure about efficiency: in a really tight loop you might want make sure the call to endof is hoisted:

#include <limits>
#include <iostream>

template <typename T>
struct omega {
    T val;
    bool isInfinite;
    operator T() { return val; }
    explicit omega(const T &v) : val(v), isInfinite(false) { }
    omega &operator++() {
        (val == std::numeric_limits<T>::max()) ? isInfinite = true : ++val;
        return *this;
    }
};

template <typename T>
bool operator==(const omega<T> &lhs, const omega<T> &rhs) {
    if (lhs.isInfinite) return rhs.isInfinite;
    return (!rhs.isInfinite) && lhs.val == rhs.val;
}
template <typename T>
bool operator!=(const omega<T> &lhs, const omega<T> &rhs) {
    return !(lhs == rhs);
}

template <typename T>
omega<T> endof(T val) { 
    omega<T> e(val);
    return ++e;
}

template <typename T>
void closed_range(T first, T last) {
    for (omega<T> i(first); i != endof(last); ++i) {
        // do something
        std::cout << i << "\n";
    }
}

int main() {
    closed_range((short)32765, std::numeric_limits<short>::max());
    closed_range((unsigned short)65533, std::numeric_limits<unsigned short>::max());
    closed_range(1, 0);
}

Output:

32765
32766
32767
65533
65534
65535

Be a bit careful using other operators on omega<T> objects. I've only implemented the absolute minimum for the demonstration, and omega<T> implicitly converts to T, so you'll find that you can write expressions which potentially throw away the "infiniteness" of omega objects. You could fix that by declaring (not necessarily defining) a full set of arithmetic operators; or by throwing an exception in the conversion if isInfinite is true; or just don't worry about it on grounds that you can't accidentally convert the result back to an omega, because the constructor is explicit. But for example, omega<int>(2) < endof(2) is true, but omega<int>(INT_MAX) < endof(INT_MAX) is false.




回答2:


My take:

// Make sure we have at least one iteration
if (begin <= end)
{
    for (T i = begin; ; ++i)
    {
        // do something

        // Check at the end *before* incrementing so this won't
        // be affected by overflow
        if (i == end)
            break;
    }
}



回答3:


This works and is fairly clear:

T i = begin;
do {
   ...
}
while (i++ < end);

If you want to catch the special case of begin >= end you need to add another if like in Steve Jessop's solution.




回答4:


template <typename T, typename F>
void closed_range(T begin, T end, F functionToPerform) 
{
    for (T i = begin; i != end; ++i) { 
        functionToPerform(i);
    }
    functionToPerform(end);
} 



回答5:


EDIT: Reworked things to more closely match the OP.

#include <iostream>
using namespace std;

template<typename T> void closed_range(T begin, T end)
{
    for( bool cont = (begin <= end); cont; )
    {
        // do something
        cout << begin << ", ";
        if( begin == end )
            cont = false;
        else
            ++begin;
    }

    // test - this should return the last element
    cout << " --  " << begin;
}
int main()
{
    closed_range(10, 20);
    return 0;
}

The output is:

10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, -- 20



来源:https://stackoverflow.com/questions/2457389/looping-on-a-closed-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!