Я бы хотел использовать цикл на основе диапазона для диапазона простых чисел, поэтому вот что я сделал:
Код: Выделить всё
//Consider prime_generator a class with both operator*, operator!= and operator< overloaded
class primes_range {
private:
unsigned int max;
public:
primes_range(unsigned int max) : max(max) {}
prime_generator begin() const {
return prime_generator(); //so this generator begin from 2 to
//infinity and beyond but of course
//all primes
}
prime_generator end() const {
prime_generator result;
for (:*result < max; ++result) {} //so this thing actually create a
//generator and increment it until it
//gives the first prime number
//above max so it basically do
//all the work that I don't
//want it to do now
return rest;
}
};
Код: Выделить всё
int main() {
for (auto && i : primes_range(10)) { //So here, this is silly because
//the range-based loop will use end()
//which will calculate all the prime
//numbers at the very beginning
//and i will increment apart from
//this starting process
cout
Подробнее здесь: [url]https://stackoverflow.com/questions/41977956/range-based-loop-use-another-operator[/url]