mFrame
載入中...
搜尋中...
無符合項目
ufm::util::Iterator< E > 類別 樣版 參考文件

迭代器類別,提供遍歷集合元素的標準介面。 更多...

#include <Iterator.h>

類別ufm::util::Iterator< E >的繼承圖:
ufm::lang::Object ufm::lang::Interface

公開方法(Public Methods)

 Iterator (ufm::lang::Iterable< E > &iterable, int index=0) noexcept
 建構 Iterator 物件
 
virtual ~Iterator (void) override
 解構 Iterator 物件
 
E & operator* () const
 解引用運算子,取得目前元素的參考
 
Iteratoroperator++ ()
 前置遞增運算子,移動到下一個元素
 
bool operator!= (const Iterator &other) const
 不等於運算子,比較兩個迭代器位置
 
bool hasNext (void) const
 檢查是否還有下一個元素
 
void next (void)
 移動迭代器到下一個元素
 
E * get (void) const
 取得目前元素的指標
 
int index (void) const
 取得目前迭代位置的索引
 
- 公開方法(Public Methods) 繼承自 ufm::lang::Object
void * operator new (size_t n)
 使用運算子 new 分配記憶體
 
void * operator new (size_t n, void *p)
 在指定記憶體上調用運算子 new
 
virtual ufm::lang::ObjectgetObject (void) override
 取得對應的 Object 物件
 
void delay (int milliseconds) const
 延遲指定的毫秒數進行執行緒等待
 
bool equals (Object *object) const
 判斷與另一物件是否為相同參照(指標型態比較)
 
bool equals (Object &object) const
 判斷與另一物件是否為相同參照(參照型態比較)
 
void wait (void) const
 使當前線程等待直到被通知
 
bool wait (int timeout) const
 等待通知或超時
 
bool yield (void) const
 讓執行緒讓渡控制權給同優先權的下一個執行緒
 
int lock (void) const
 核心鎖定,鎖定期間禁止線程切換
 
int unlock (void) const
 核心解鎖
 
ufm::sys::ThreadcurrentThread (void) const
 取得當前執行緒指標
 
virtual int hashcode (void) const
 返回對象的哈希碼值。支持這種方法是為了散列表,如HashMap提供的那樣。
 
- 公開方法(Public Methods) 繼承自 ufm::lang::Interface
virtual ~Interface (void)=default
 虛擬析構函式
 

詳細描述

template<class E = void*>
class ufm::util::Iterator< E >

迭代器類別,提供遍歷集合元素的標準介面。

樣版參數
E迭代器元素的類型,預設為 void*

Iterator 類別實作了標準的迭代器模式,提供了一種統一的方式 來遍歷各種集合類型中的元素,而無需了解集合的內部實作細節。 它支援 C++ 標準的迭代器語法,可以與範圍式 for 迴圈配合使用。

主要功能:
  • 提供標準的迭代器語法支援
  • 支援前置遞增運算子(++it)
  • 支援解引用運算子(*it)
  • 提供元素存取和位置查詢
  • 支援迭代器比較操作
使用場景:
  • 遍歷自定義集合類型
  • 實作範圍式 for 迴圈支援
  • 提供統一的元素存取介面
  • 隱藏集合內部實作細節
使用範例:
// 實作一個簡單的可迭代集合
class SimpleList : public ufm::lang::Iterable<int> {
private:
std::vector<int> data;
public:
// 添加元素
void add(int value) {
data.push_back(value);
}
// 實作 Iterable 介面
void forEach(ufm::func::Consumer<int&>& consumer) override {
for (auto& item : data) {
consumer.accept(item);
}
}
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;
}
// 提供 begin 和 end 方法支援範圍式 for 迴圈
return ufm::util::Iterator<int>(*this, 0);
}
return ufm::util::Iterator<int>(*this, -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;
// 方法 1: 使用傳統迭代器語法
while (it.hasNext()) {
std::cout << "元素: " << *it << std::endl;
++it;
}
// 方法 2: 使用範圍式 for 迴圈(需要 begin/end 方法)
std::cout << "使用範圍式 for 迴圈:" << std::endl;
for (auto& element : myList) {
std::cout << "元素: " << element << std::endl;
// 可以修改元素
element *= 2;
}
// 方法 3: 手動控制迭代過程
std::cout << "手動控制迭代:" << std::endl;
ufm::util::Iterator<int> controlledIt(myList);
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; // true
std::cout << "it1 != end: " << (it1 != end) << std::endl; // true
// 移動迭代器
++it1;
std::cout << "移動 it1 後:" << std::endl;
std::cout << "it1 != it2: " << (it1 != it2) << std::endl; // true
std::cout << "it1 位置: " << it1.index() << std::endl;
std::cout << "it2 位置: " << it2.index() << std::endl;
}
};
// 測試比較功能
IteratorComparison::demonstrateComparison(myList);
// 安全迭代範例
class SafeIterator {
public:
template<typename T>
static void safeIterate(ufm::lang::Iterable<T>& collection) {
try {
ufm::util::Iterator<T> it(collection);
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>
static int count(ufm::lang::Iterable<T>& collection) {
int count = 0;
ufm::util::Iterator<T> it(collection);
while (it.hasNext()) {
count++;
++it;
}
return count;
}
template<typename T>
static T* find(ufm::lang::Iterable<T>& collection, const T& value) {
ufm::util::Iterator<T> it(collection);
while (it.hasNext()) {
T* current = it.get();
if (current && *current == value) {
return current;
}
++it;
}
return nullptr;
}
template<typename T>
static bool any(ufm::lang::Iterable<T>& collection,
std::function<bool(const T&)> predicate) {
ufm::util::Iterator<T> it(collection);
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
參閱
ufm::lang::Iterable
ufm::lang::Object
1.0.0
範例
F:/mframe/doxy-document/src/mframe/src/ufm/util/Span.h.

建構子與解構子說明文件

◆ Iterator()

template<class E = void*>
ufm::util::Iterator< E >::Iterator ( ufm::lang::Iterable< E > & iterable,
int index = 0 )
inlinenoexcept

建構 Iterator 物件

參數
iterable要迭代的可迭代集合
index起始索引位置,預設為 0

◆ ~Iterator()

template<class E = void*>
virtual ufm::util::Iterator< E >::~Iterator ( void )
inlineoverridevirtual

解構 Iterator 物件

釋放迭代器相關資源。

函式成員說明文件

◆ get()

template<class E = void*>
E * ufm::util::Iterator< E >::get ( void ) const
inline

取得目前元素的指標

傳回值
E* 目前元素的指標,如果無效則為 nullptr

◆ hasNext()

template<class E = void*>
bool ufm::util::Iterator< E >::hasNext ( void ) const
inline

檢查是否還有下一個元素

傳回值
true 尚有更多元素,false 已無更多元素

◆ index()

template<class E = void*>
int ufm::util::Iterator< E >::index ( void ) const
inline

取得目前迭代位置的索引

傳回值
int 目前位置索引

◆ next()

template<class E = void*>
void ufm::util::Iterator< E >::next ( void )
inline

移動迭代器到下一個元素

例外
如果已到達集合結尾則拋出例外

◆ operator!=()

template<class E = void*>
bool ufm::util::Iterator< E >::operator!= ( const Iterator< E > & other) const
inline

不等於運算子,比較兩個迭代器位置

參數
other另一個迭代器
傳回值
true 位置不同,false 位置相同

◆ operator*()

template<class E = void*>
E & ufm::util::Iterator< E >::operator* ( ) const
inline

解引用運算子,取得目前元素的參考

傳回值
E& 目前元素的參考

◆ operator++()

template<class E = void*>
Iterator & ufm::util::Iterator< E >::operator++ ( )
inline

前置遞增運算子,移動到下一個元素

傳回值
Iterator& 迭代器本身的參考

此類別(class) 文件是由下列檔案中產生: