Skip to content

Instantly share code, notes, and snippets.

@goldsborough
Created April 2, 2015 01:10
Show Gist options
  • Save goldsborough/4d8cbd8f1f2127c4b450 to your computer and use it in GitHub Desktop.
Save goldsborough/4d8cbd8f1f2127c4b450 to your computer and use it in GitHub Desktop.
Python range() in C++
class range
{
public:
typedef std::vector<long>::iterator iterator;
typedef std::vector<long>::const_iterator const_iterator;
range(long end)
: range(0, end)
{ }
range(long begin, long end, long step = 1)
{
long diff = end - begin;
if ((diff > 0 && step > 0) ||
(diff < 0 && step < 0))
{
while(begin != end)
{
range_.push_back(begin);
begin += step;
}
}
}
iterator begin()
{
return range_.begin();
}
const_iterator begin() const
{
return range_.begin();
}
iterator end()
{
return range_.end();
}
const_iterator end() const
{
return range_.end();
}
private:
std::vector<long> range_;
};
int main(int argc, char * argv [])
{
for (auto i : range(10, 5, -1))
{
std::cout << i << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment