If you find yourself having to do this a lot I recommend you use the Boost.RangeEx library:
#include
#include
#include
#include
It's been accepted into Boost but it doesn't come with the official distribution yet. Until it does you can download it from the Boost Vault (download link to zip file).