fork download
  1. /*
  2. Create an iterator class called BitBucket that just absorbs whatever you send to
  3. it without writing it anywhere.
  4. */
  5.  
  6. #include <iostream>
  7. #include <iterator>
  8. #include <vector>
  9. #include <algorithm>
  10.  
  11. using namespace std;
  12.  
  13. template<class InputIter, class _Tp>
  14. class BitBucket : public std::iterator<
  15. std::input_iterator_tag, _Tp, std::ptrdiff_t> {
  16. InputIter first;
  17. _Tp t;
  18. public:
  19. BitBucket(InputIter begin)
  20. : first(begin){}
  21. BitBucket() : first(NULL) {} // End sentinel
  22. // Prefix increment:
  23. BitBucket& operator++() {
  24. first++;
  25. return *this;
  26. }
  27. BitBucket operator++(int) {
  28. BitBucket b = *this;
  29. ++first;
  30. return b;
  31. }
  32. _Tp& operator *() { return t; }
  33. bool operator == ( const BitBucket&b ) const{ return first == b.first; }
  34. bool operator != ( const BitBucket&b ) const{ return !(*this==b); }
  35. };
  36.  
  37. struct RandGen {
  38. int operator()() { return std::rand() %20; }
  39. };
  40. int main(int argc, char**argv) {
  41. vector<int> v;
  42. generate_n(back_inserter(v),10,RandGen());
  43. BitBucket<vector<int>::iterator,int> b1(v.begin()),b2(v.end());
  44. while (b1!=b2) *b1++ = 13;
  45. copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
  46. return 0;
  47. }
Success #stdin #stdout 0s 5268KB
stdin
Standard input is empty
stdout
3 6 17 15 13 15 6 12 9 1