C++ Iterator Library iterator

C++ Iterator Library iterator
返回C++,Iterator,Library,iterator常用代码示例代码片段
#include <iostream>     
#include <iterator>     

class MyIterator : public std::iterator<std::input_iterator_tag, int> {
   int* p;
public:
   MyIterator(int* x) :p(x) {}
   MyIterator(const MyIterator& mit) : p(mit.p) {}
   MyIterator& operator++() {++p;return *this;}
   MyIterator operator++(int) {MyIterator tmp(*this); operator++(); return tmp;}
   bool operator==(const MyIterator& rhs) {return p==rhs.p;}
   bool operator!=(const MyIterator& rhs) {return p!=rhs.p;}
   int& operator*() {return *p;}
};

int main () {
   int numbers[] = {1,2,3,4,5};
   MyIterator from(numbers);
   MyIterator until(numbers+5);
   for (MyIterator it = from; it!=until; it++)
      std::cout << *it << ' ';
   std::cout << '\n';

  return 0;
}
原文链接:https://rumenz.com/examples/Cpp11/cplusplus-iterator-library-iterator.html

【Linux常用命令速查手册】关注【入门小站】,后台回复 「1001」 自取。

点击下面标题即可获取对应PDF资料
↑回到顶部↑
入门小站 @2018