Mathematica has a function called Range[]
that does the following:
Range[0, 10]
Range[-10, 0]
Ant it prints:
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0}
Does C++ have such a function?
None in the standard library, but from boost::range
:
#include <iostream>
#include <iterator>
#include <boost/range/irange.hpp>
#include <boost/range/algorithm/copy.hpp>
int main()
{
boost::copy(boost::irange(0, 11),
std::ostream_iterator<int>(std::cout, " "));
}
Output: 0 1 2 3 4 5 6 7 8 9 10
Seems easy enough to create one.
std::vector<int> range(int from, int to) {
std::vector<int> result;
result.reserve(to-from+1);
for (int i = from; i <= to; ++i) {
result.push_back(i);
}
return result;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With