template<typename E>
private:
E* elements;
int capacity;
int count;
public:
SimpleCollection(int cap) : capacity(cap), count(0) {
elements = new E[capacity];
}
void clear(void) override {
count = 0;
}
bool isEmpty(void) const override {
return count == 0;
}
int size(void) const override {
return count;
}
for (int i = 0; i < count; ++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);
collection.add(10);
collection.add(20);
collection.add(30);
bool hasElement = collection.contains(20);
collection.remove(20);
collection.clear();
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