Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 523477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:27:59+00:00 2026-05-13T08:27:59+00:00

Is there C++ equivalent for python Xrange generator in either STL or boost? xrange

  • 0

Is there C++ equivalent for python Xrange generator in either STL or boost?

xrange basically generates incremented number with each call to ++ operator.
the constructor is like this:

xrange(first, last, increment)

was hoping to do something like this using boost for each:

foreach(int i, xrange(N))

I. am aware of the for loop. in my opinion they are too much boilerplate.

Thanks

my reasons:

my main reason for wanting to do so is because i use speech to text software, and programming loop usual way is difficult, even if using code completion. It is much more efficient to have pronounceable constructs.

many loops start with zero and increment by one, which is default for range. I find python construct more intuitive

 for(int i = 0; i < N; ++i)
 foreach(int i, range(N))

functions which need to take range as argument:

 Function(int start, int and, int inc);
 function(xrange r);

I understand differences between languages, however if a particular construct in python is very useful for me and can be implemented efficiently in C++, I do not see a reason not to use it. For each construct is foreign to C++ as well however people use it.

I put my implementation at the bottom of the page as well the example usage.

in my domain i work with multidimensional arrays, often rank 4 tensor. so I would often end up with 4 nested loops with different ranges/increments to compute normalization, indexes, etc. those are not necessarily performance loops, and I am more concerned with correctness readability and ability to modify.

for example

int function(int ifirst, int ilast, int jfirst, int jlast, ...);
versus
int function(range irange, range jrange, ...);

In the above, if different strids are needed, you have to pass more variables, modify loops, etc. eventually you end up with a mass of integers/nearly identical loops.

foreach and range solve my problem exactly. familiarity to average C++ programmer is not high on my list of concerns – problem domain is a rather obscure, there is a lot of meta-programming, SSE intrinsic, generated code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:27:59+00:00Added an answer on May 13, 2026 at 8:27 am

    Boost has counting_iterator as far as I know, which seems to allow only incrementing in steps of 1. For full xrange functionality you might need to implement a similar iterator yourself.

    All in all it could look like this (edit: added an iterator for the third overload of xrange, to play around with boost’s iterator facade):

    #include <iostream>
    #include <boost/iterator/counting_iterator.hpp>
    #include <boost/range/iterator_range.hpp>
    #include <boost/foreach.hpp>
    #include <boost/iterator/iterator_facade.hpp>
    #include <cassert>
    
    template <class T>
    boost::iterator_range<boost::counting_iterator<T> > xrange(T to)
    {
        //these assertions are somewhat problematic:
        //might produce warnings, if T is unsigned
        assert(T() <= to);
        return boost::make_iterator_range(boost::counting_iterator<T>(0), boost::counting_iterator<T>(to));
    }
    
    template <class T>
    boost::iterator_range<boost::counting_iterator<T> > xrange(T from, T to)
    {
        assert(from <= to);
        return boost::make_iterator_range(boost::counting_iterator<T>(from), boost::counting_iterator<T>(to));
    }
    
    //iterator that can do increments in steps (positive and negative)
    template <class T>
    class xrange_iterator:
        public boost::iterator_facade<xrange_iterator<T>, const T, std::forward_iterator_tag>
    {
        T value, incr;
    public:
        xrange_iterator(T value, T incr = T()): value(value), incr(incr) {}
    private:
        friend class boost::iterator_core_access;
        void increment() { value += incr; }
        bool equal(const xrange_iterator& other) const
        {
            //this is probably somewhat problematic, assuming that the "end iterator"
            //is always the right-hand value?
            return (incr >= 0 && value >= other.value) || (incr < 0 && value <= other.value);
        }
        const T& dereference() const { return value; }
    };
    
    template <class T>
    boost::iterator_range<xrange_iterator<T> > xrange(T from, T to, T increment)
    {
        assert((increment >= T() && from <= to) || (increment < T() && from >= to));
        return boost::make_iterator_range(xrange_iterator<T>(from, increment), xrange_iterator<T>(to));
    }
    
    int main()
    {
        BOOST_FOREACH(int i, xrange(10)) {
            std::cout << i << ' ';
        }
        BOOST_FOREACH(int i, xrange(10, 20)) {
            std::cout << i << ' ';
        }
        std::cout << '\n';
        BOOST_FOREACH(int i, xrange(0, 46, 5)) {
            std::cout << i << ' ';
        }
        BOOST_FOREACH(int i, xrange(10, 0, -1)) {
            std::cout << i << ' ';
        }
    }
    

    As others are saying, I don’t see this buying you much over a normal for loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 313k
  • Answers 313k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I had a hard time trying to figure out your… May 13, 2026 at 10:46 pm
  • Editorial Team
    Editorial Team added an answer The Core Data data model needs to be defined at… May 13, 2026 at 10:46 pm
  • Editorial Team
    Editorial Team added an answer You need to correctly add escaped string delimiters around the… May 13, 2026 at 10:46 pm

Related Questions

I know there are many tutorials out there for getting started in C. However
I'm trying to learn myself some C++ from scratch at the moment. I'm well-versed
Using Google protobuf, I am saving my serialized messaged data to a file -
I need assistance finding a delivery method that best fulfills the following requirements: We
I have a file with data listed as follows: 0, 2, 10 10, 8,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.