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 7792027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:04:11+00:00 2026-06-01T22:04:11+00:00

I’m trying (as an exercise) to create a simple numeric range class in C++.

  • 0

I’m trying (as an exercise) to create a simple numeric range class in C++. It will let you iterate through evenly spaced doubles (like the numpy/Python arange):

What I’d like to do (but with an iterator):

double lower = ..., upper = ..., delta = ...;
for (double val = lower; val < upper; val += delta)
{
   // do something with val
   f(val);
}
// include the last val to guarantee upper is included or exceeded
f(val); // do something with val

Desired equivalent iterator code:

double lower = ..., upper = ..., delta = ...;
NumericRange nr(lower, upper, delta);
for (NumericRange::const_iterator iter = nr.begin(); iter != nr.end(); iter++)
{
    f(*iter);
}

I’d like my iterator to be compatible with STL iterators so I can reuse code (iterating through a NumericRange should be equivalent to iterating through std::vector).

I’ve had success simply storing the values in a std::vector (and then using the std::vector’s iterator). This is how everything I’ve found online has solved this problem. However, it really isn’t necessary to store the entire list.

Is there a way to avoid storing the entire set of values? Is there some iterable class I can inherit from and override ++, ==, etc. to get the desired effect without storing the std::vector<double>?

(I’d really like to know how to do this without BOOST, even though it’s great. I’m asking this because I’d like to learn how to write (from scratch) something like a BOOST solution. I definitely know that part of software engineering is using the tools created by others, but I really want to learn how those tools are designed and built.)

My iterable NumericRange class (using std::vector<double> internally):

class NumericRange
{
protected:
  double lower, upper, delta;
  std::vector<double> sorted_range;
public:
  typedef std::vector<double>::const_iterator const_iterator;
  NumericRange()
  {
    lower = upper = delta = std::numeric_limits<double>::quiet_NaN();
    // vector is constructed empty
  }
  NumericRange(double lower_param, double upper_param, double delta_param)
  {
    lower = lower_param;

    upper = upper_param;
    delta = delta_param;
    assert(upper_param > lower_param);

    double val;
    // note: can be much faster without push_back
    for (val = lower_param; val < upper_param; val += delta_param)
      {
    sorted_range.push_back(val);
      }
    // ensure the upper_value is contained or surpassed
    sorted_range.push_back(val);
  }
  // to prevent comparison of the entire vector
  bool operator ==(const NumericRange & rhs) const
  {
    return lower == rhs.lower && upper == rhs.upper && delta == rhs.delta;
  }
  // note: this class doesn't really need to store the values in a
  // vector, but it makes the iterator interface much easier.
  const_iterator begin() const
  {
    return sorted_range.begin();
  }
  const_iterator end() const
  {
    return sorted_range.end();
  }
  double get_lower() const
  {
    return lower;
  }
  double get_upper() const
  {
    return upper;
  }
  double get_delta() const
  {
    return delta;
  }
  size_t size() const
  {
    return sorted_range.size();
  }
  void print() const
  {
    std::cout << "[ " << lower << " : " << upper << ": +=" << delta << " ]" << std::endl;
  }
};
  • 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-06-01T22:04:12+00:00Added an answer on June 1, 2026 at 10:04 pm

    Is there some iterable class I can inherit from and override ++, ==, etc. to get the desired effect without storing the std::vector<double>?

    Yes, there is. Its name is std::iterator<std::input_iterator_tag, double>.

    Here is a start for you, using int. To save space in my brain, I use the same class to represent both the range and the iterator.

    #include <iterator>
    #include <iostream>
    
    struct NumericRange : public std::iterator< std::input_iterator_tag, int >
    {
      int current, fini, delta;
      typedef NumericRange iterator;
      typedef iterator const_iterator;
      iterator begin() { return *this; }
      iterator end() { return iterator(fini, fini, delta); }
      iterator& operator++() { current += delta; return *this; }
      iterator operator++(int) { iterator result(*this); ++*this; return result; }
      int operator*() const { return current; }
      NumericRange(int start, int fini, int delta) 
        : current(start), fini(fini), delta(delta)
      {
      }
      bool operator==(const iterator& rhs) {
        return rhs.current == current;
      }
      bool operator!=(const iterator& rhs) {
        return !(*this == rhs);
      }
    };
    
    void f(int i, int j) {
      std::cout << i << " " << j << "\n";
    }
    
    int main () {
      int lower = 4, upper = 14, delta = 5;
      NumericRange nr(lower, upper, delta);
      for (NumericRange::const_iterator iter = nr.begin(); iter != nr.end(); iter++)
      {
          f(*iter, *nr.end());
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am doing a simple coin flipping experiment for class that involves flipping a
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.