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

集合介面,定義集合層次結構的根介面。 更多...

#include <Collection.h>

類別ufm::util::Collection< E >的繼承圖:
ufm::lang::Iterable< E > ufm::util::Container ufm::util::Pool ufm::util::Queue< E >

額外的繼承成員

- 公開方法(Public Methods) 繼承自 ufm::lang::Iterable< E >
virtual void forEach (ufm::func::Consumer< E & > &action) override
 遍歷集合中所有元素,對每個元素執行指定操作。 若所有元素處理完畢或操作中發生異常則停止。
 
virtual E * elementAt (int index) const override
 獲取集合中指定索引處的元素。
 
virtual int nextIndex (int index) const override
 返回當前索引的下一個有效索引。
 
virtual ufm::util::Iterator< E > begin (void) override
 返回一個迭代器,用於遍歷集合中的元素
 
virtual ufm::util::Iterator< E > end (void) override
 返回一個迭代器,用於遍歷集合的結尾
 
- 公開方法(Public Methods) 繼承自 ufm::util::Container
virtual void clear (void) override
 從此集合中刪除所有元素(可選操作)
 
virtual bool isEmpty (void) const override
 檢查此集合是否不包含任何元素
 
virtual int size (void) const override
 返回此集合中的元素數量
 

詳細描述

template<typename E = void*>
struct ufm::util::Collection< E >

集合介面,定義集合層次結構的根介面。

樣版參數
E此集合中保存的元素類型,預設為 void*

Collection 是集合系統的基礎介面,定義了所有集合類型的共通操作。 集合表示一組物件(稱為元素),提供了元素的儲存、存取和遍歷功能。 不同的集合實作可能允許或不允許重複元素,可能是有序或無序的。

集合類型說明:
  • 有序集合:元素有特定的順序(如 List)
  • 無序集合:元素沒有特定順序(如 Set)
  • 允許重複:同一元素可以多次出現(如 List)
  • 不允許重複:每個元素只能出現一次(如 Set)
使用範例:
// 定義一個整數集合實作
template<typename E>
class SimpleCollection : public ufm::util::Collection<E> {
private:
E* elements; // 使用陣列儲存元素
int capacity; // 陣列容量
int count; // 當前元素數量
public:
// 建構函數
SimpleCollection(int cap) : capacity(cap), count(0) {
elements = new E[capacity];
}
// 實作 Container 介面
void clear(void) override {
count = 0;
}
bool isEmpty(void) const override {
return count == 0;
}
int size(void) const override {
return count;
}
// 實作 Iterable 介面
void forEach(ufm::func::Consumer<E&>& consumer) override {
for (int i = 0; i < count; ++i) {
consumer.accept(elements[i]);
}
}
E* elementAt(int index) const override {
if (index >= 0 && index < count) {
return &elements[index];
}
return nullptr;
}
int nextIndex(int index) const override {
if (index < 0) return 0;
if (index + 1 < count) return index + 1;
return -1;
}
// 集合特定方法
bool add(const E& element) {
if (count < capacity) {
elements[count++] = element;
return true;
}
return false;
}
bool remove(const E& element) {
for (int i = 0; i < count; ++i) {
if (elements[i] == element) {
// 移動後續元素
for (int j = i; j < count - 1; ++j) {
elements[j] = elements[j + 1];
}
--count;
return true;
}
}
return false;
}
bool contains(const E& element) const {
for (int i = 0; i < count; ++i) {
if (elements[i] == element) {
return true;
}
}
return false;
}
};
// 使用集合
SimpleCollection<int> collection(10); // 容量為 10 的集合
// 添加元素
collection.add(10);
collection.add(20);
collection.add(30);
// 檢查集合狀態
ufm::lang::System::out().print("集合大小: ");
ufm::lang::System::out().println(collection.size()); // 3
ufm::lang::System::out().print("是否為空: ");
ufm::lang::System::out().println(collection.isEmpty() ? "true" : "false"); // false
// 檢查是否包含特定元素
bool hasElement = collection.contains(20); // true
// 移除元素
collection.remove(20);
ufm::lang::System::out().print("移除後大小: ");
ufm::lang::System::out().println(collection.size()); // 2
// 清空集合
collection.clear();
ufm::lang::System::out().print("清空後是否為空: ");
ufm::lang::System::out().println(collection.isEmpty() ? "true" : "false"); // true
// 作為 Container 使用
ufm::util::Container& container = collection;
// 作為 Iterable 使用
ufm::lang::Iterable<int>& iterable = collection;
PrintStream & println(void)
印出換行字元。
PrintStream & print(bool b, bool newLine=false)
印出布林值,可選擇是否換行。
static ufm::io::PrintStream & out(void)
獲取系統輸出緩衝區
Definition System.h:127
[Interface] 消費者函數式介面模板
Definition Consumer.h:43
virtual void accept(T &t) override
對給定的參數執行操作
[Interface] 定義可迭代集合介面
Definition Iterable.h:49
集合介面,定義集合層次結構的根介面。
Definition Collection.h:171
容器介面,定義所有集合容器的基本操作。
Definition Container.h:176
參閱
ufm::util::Container
ufm::lang::Iterable
ufm::util::Set
ufm::util::Queue
1.0.0

此結構(structure) 文件是由下列檔案中產生: