Skip to main content
added 346 characters in body
Source Link
kevin cline
  • 33.7k
  • 3
  • 72
  • 142

The fastest way to do this is to not do it. Create Here's an outline of a new collection class that will iterate overlooks like a rangeread-only array of consecutive integers, without storing a copy of each one.

class range {
private:
  int low, high;
public:
  range_iteratortypedef int iterator;
  range(int low, int high) { this.low = low; this.high = high; }
  iterator begin(); { return low; }
  range_iteratoriterator end() { return high + 1; }
  int operator[](int index) { return low + index; }
}
...
range r(1, 1000);
for (r::iterator i = r.begin(); i != r.end(); ++i) { ... }

If you need a writable collection, then the answer depends on how you want to modify it after creation.

The fastest way to do this is to not do it. Create a new collection class that will iterate over a range of integers, without storing a copy of each one.

class range {
private:
  int low, high;
public:
  range_iterator begin();
  range_iterator end();
}

The fastest way to do this is to not do it. Here's an outline of a class that looks like a read-only array of consecutive integers.

class range {
private:
  int low, high;
public:
  typedef int iterator;
  range(int low, int high) { this.low = low; this.high = high; }
  iterator begin() { return low; }
  iterator end() { return high + 1; }
  int operator[](int index) { return low + index; }
}
...
range r(1, 1000);
for (r::iterator i = r.begin(); i != r.end(); ++i) { ... }

If you need a writable collection, then the answer depends on how you want to modify it after creation.

Source Link
kevin cline
  • 33.7k
  • 3
  • 72
  • 142

The fastest way to do this is to not do it. Create a new collection class that will iterate over a range of integers, without storing a copy of each one.

class range {
private:
  int low, high;
public:
  range_iterator begin();
  range_iterator end();
}