With C++17 the compiler should be able to deduce the template type automatically by using the following:
template <typename Iterator>
class ConstVectorSubrange {
public:
using T = typename Iterator::value_type;
// ...
}
int main()
{
std::vector<int>vector v{ 1, 2, 3, 4, 5, 6, 7, 8 };
ConstVectorSubrange range(v.begin() + 2, v.end() - 2);
}
I'm not sure why you have included <iterator> and failed to include <cassert>?
Good:
I like that you can now use the foreach loop:
std::vector<int>vector v{ 7, 7, 3, 4, 5, 6, 7, 7, 8, 9 };
ConstVectorSubrange vsr(v.begin() + 4, v.end());
for (int i : vsr)
std::cout << i << "\n";
And all the cool stuff from <algorithm>
std::cout << std::count(vsr.begin(), vsr.end(), 7) << "\n";
Ideas:
It would be pretty cool if you could include a step or stride to have a slice of the vector. Something like this:
std::vector<int>vector v{ 1, 2, 3, 4, 5, 6 };
std::size_t step = 2;
ConstVectorSubrange r(v.begin() + 1, v.end(), step);
for (int i : r)
std::cout << i << "\n";
// expected results 2, 4, 6