private:
std::vector<int> data;
public:
void add(int value) {
data.push_back(value);
}
for (auto& item : data) {
}
}
int* elementAt(int index) const override {
if (index >= 0 && index < data.size()) {
return const_cast<int*>(&data[index]);
}
return nullptr;
}
int nextIndex(int index) const override {
if (index < 0) return data.empty() ? -1 : 0;
if (index + 1 < data.size()) return index + 1;
return -1;
}
}
}
size_t size() const {
return data.size();
}
};
SimpleList myList;
myList.add(10);
myList.add(20);
myList.add(30);
myList.add(40);
myList.add(50);
std::cout << "使用迭代器遍歷集合:" << std::endl;
while (it.hasNext()) {
std::cout << "元素: " << *it << std::endl;
++it;
}
std::cout << "使用範圍式 for 迴圈:" << std::endl;
for (auto& element : myList) {
std::cout << "元素: " << element << std::endl;
element *= 2;
}
std::cout << "手動控制迭代:" << std::endl;
for (int i = 0; i < 3 && controlledIt.hasNext(); ++i) {
std::cout << "前3個元素: " << *controlledIt << std::endl;
++controlledIt;
}
class IteratorComparison {
public:
static void demonstrateComparison(SimpleList& list) {
auto it1 = list.begin();
auto it2 = list.begin();
auto end = list.end();
std::cout << "迭代器比較演示:" << std::endl;
std::cout << "it1 == it2: " << (!(it1 != it2)) << std::endl;
std::cout << "it1 != end: " << (it1 != end) << std::endl;
++it1;
std::cout << "移動 it1 後:" << std::endl;
std::cout << "it1 != it2: " << (it1 != it2) << std::endl;
std::cout << "it1 位置: " << it1.index() << std::endl;
std::cout << "it2 位置: " << it2.index() << std::endl;
}
};
IteratorComparison::demonstrateComparison(myList);
class SafeIterator {
public:
template<typename T>
try {
std::cout << "安全迭代開始:" << std::endl;
while (it.hasNext()) {
try {
T* element = it.get();
if (element != nullptr) {
std::cout << "安全獲取元素: " << *element << std::endl;
}
++it;
} catch (const std::exception& e) {
std::cout << "迭代過程中發生錯誤: " << e.what() << std::endl;
break;
}
}
} catch (const std::exception& e) {
std::cout << "迭代器初始化失敗: " << e.what() << std::endl;
}
}
};
SafeIterator::safeIterate(myList);
class IteratorAlgorithms {
public:
template<typename T>
int count = 0;
while (it.hasNext()) {
count++;
++it;
}
return count;
}
template<typename T>
while (it.hasNext()) {
T* current = it.get();
if (current && *current == value) {
return current;
}
++it;
}
return nullptr;
}
template<typename T>
std::function<bool(const T&)> predicate) {
while (it.hasNext()) {
T* current = it.get();
if (current && predicate(*current)) {
return true;
}
++it;
}
return false;
}
};
int elementCount = IteratorAlgorithms::count(myList);
std::cout << "集合元素總數: " << elementCount << std::endl;
int* found = IteratorAlgorithms::find(myList, 60);
if (found) {
std::cout << "找到元素: " << *found << std::endl;
}
bool hasLargeNumber = IteratorAlgorithms::any(myList, [](const int& value) {
return value > 50;
});
std::cout << "是否有大於50的數字: " << (hasLargeNumber ? "是" : "否") << std::endl;
迭代器類別,提供遍歷集合元素的標準介面。
Definition Iterator.h:257
[Interface] 消費者函數式介面模板
Definition Consumer.h:43
virtual void accept(T &t) override
對給定的參數執行操作
[Interface] 定義可迭代集合介面
Definition Iterable.h:49