fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <iterator>
  4. #include <utility>
  5.  
  6. namespace hoge {
  7.  
  8. // intとdoubleを読み込むイテレータ
  9. class my_iter
  10. : public std::iterator< std::forward_iterator_tag, my_iter> {
  11.  
  12. public:
  13. // Constructors
  14. my_iter()
  15. : stream_(0), eof_(true), values_(std::pair< int, double>()) {
  16. // デフォルトはeofとする
  17. }
  18.  
  19. explicit my_iter(std::istream *stream)
  20. : stream_(stream), eof_(false), values_(std::pair< int, double>()) {
  21. read();
  22. }
  23.  
  24. // Copy
  25. my_iter(my_iter const &other)
  26. : stream_(other.stream_), eof_(other.eof_), values_(other.values_) {
  27. }
  28.  
  29. my_iter& operator=(my_iter const &other) {
  30. if(this != &other) {
  31. my_iter(other);
  32. std::swap(*this, other);
  33. }
  34. return *this;
  35. }
  36.  
  37. // Move
  38. my_iter(my_iter &&other)
  39. : stream_(0), eof_(true), values_(std::pair< int, double>()) {
  40. std::swap(stream_, other.stream_);
  41. std::swap(eof_, other.eof_);
  42. std::swap(values_, other.values_);
  43. }
  44.  
  45. my_iter& operator=(my_iter &&other) {
  46. if(this != &other)
  47. std::swap(*this, other);
  48. return *this;
  49. }
  50.  
  51. // 前置
  52. my_iter& operator++() {
  53. read();
  54. return *this;
  55. }
  56.  
  57. // デリファレンス
  58. std::pair< int, double>& operator*() {
  59. return values_;
  60. }
  61.  
  62. // 比較
  63. friend bool operator==(my_iter const &lhs, my_iter const &rhs) {
  64. return lhs.eof_ && rhs.eof_; // 両方がeofだったらイコール
  65. }
  66.  
  67. friend bool operator!=(my_iter const &lhs, my_iter const &rhs) {
  68. return !(lhs == rhs);
  69. }
  70.  
  71. private:
  72.  
  73. // 読み込み
  74. void read() {
  75. if(!eof_)
  76. if(!(*stream_).eof()) {
  77. *stream_ >> values_.first;
  78. if(!(*stream_).eof())
  79. *stream_ >> values_.second;
  80. else
  81. eof_ = true;
  82. } else
  83. eof_ = true;
  84. }
  85.  
  86. std::istream *stream_; // ストリーム
  87. bool eof_; // eofに達したかどうか
  88. std::pair< int, double> values_; // 読み込んだ値
  89. };
  90.  
  91. } // namespace hoge
  92.  
  93. // ---- MAIN ---
  94.  
  95. using namespace std;
  96. using namespace hoge;
  97. int main() {
  98. int i=0;
  99.  
  100. my_iter it(&cin);
  101. my_iter it_end;
  102.  
  103. for_each(it, it_end,
  104. [&i](pair< int, double> const &v) {
  105. ++i;
  106. if(v.second < 0.0)
  107. cout << "fail (line=" << i << ")" << endl;
  108. else
  109. cout << v.first << " " << v.second << endl;
  110. }
  111. );
  112. }
  113.  
Success #stdin #stdout 0s 2964KB
stdin
-1 0.2545317407010831
-1 0.4043827251661717
-1 0.0141552946340191
stdout
-1 0.254532
-1 0.404383
-1 0.0141553