#include <iostream>
class MyCollection {
private:
int data[5] = {10, 20, 30, 40, 50};
public:
class Iterator {
private:
int* ptr;
public:
Iterator(int* p) : ptr(p) {}
int operator*() const { return *ptr; }
Iterator& operator++() { ++ptr; return *this; }
bool operator!=(const Iterator& other) const { return ptr != other.ptr; }
};
Iterator begin() { return Iterator(data); }
Iterator end() { return Iterator(data + 5); }
};
int main() {
MyCollection collection;
for (auto it = collection.begin(); it != collection.end(); ++it) {
std::cout << *it << " ";
}
// output: 10 20 30 40 50
}
위와 같이 약간의 복잡한 코드구조를 가지고있다.