008
21.07.2006, 15:34 Uhr
(un)wissender
Niveauwart
|
So geht es in C++ .
C++: |
#include <algorithm> #include <iostream> #include <iterator>
bool is_prime(unsigned x) { if(x < 2) return false; if(x < 4) return true; if(x % 2 == 0 || x % 3 == 0) return false; for(unsigned i = 5, j = 4; i * i <= x; i += (j = 6 - j)) { if(x % i == 0) return false; } return true; }
class digitIterator { public: explicit digitIterator(unsigned i) : _current(i) { } digitIterator operator++(int) { digitIterator t(_current++); return t; } digitIterator& operator++() { ++_current; return *this; } bool operator==(digitIterator const & other) const { return _current == other._current; } bool operator!=(digitIterator const & other) const { return !(*this == other); } unsigned operator*() const { return _current; }
private: unsigned _current; };
template<class InputIterator, class OutputIterator, class Pred> OutputIterator copy_if( InputIterator first, InputIterator last, OutputIterator destBeg, Pred pred ) { while(first != last) { if(pred(*first)) { *destBeg = *first; ++destBeg; } ++first; } } int main() { copy_if( digitIterator(0), digitIterator(101), std::ostream_iterator<unsigned>(std::cout, " "), &is_prime); }
|
-- Wer früher stirbt ist länger tot. |