为STL容器添加专门的功能
Adding specialized functionality to STL containers
我有一个非常特殊的场景,我正在尝试为"list"添加功能。。。
#include <list>
template <typename T>
class ShortList : public std::list<T> {
private:
unsigned short max_items;
public:
// Getter and setter methods
unsigned short getMaxItems (void) {
return this.max_items;
}
void setMaxItems (unsigned short max_items) {
this.max_items = max_items;
return;
}
// Work methods
void insertItemSorted (T item) {
std::list<T>::iterator i, e = this.short_list.end();
// Insertion sort O(n*max_items)
for ( i = this.short_list.begin() ; i != e ; ++i ) {
// Insert if smaller
if ( item.getDiff() < (*i).getDiff() ) {
this.short_list.insert(i, item);
// Restrict list size to max items
if ( this.short_list.size() > this.max_items ) {
this.short_list.erase(this.short_list.end());
}
}
}
return;
}
}
我不明白我在这里做错了什么。以下是我的编译错误。。。
ShortList.cpp: In member function 'int ShortList<T>::insertItemSorted(T)':
ShortList.cpp:21: error: expected `;' before 'i'
ShortList.cpp:24: error: 'i' was not declared in this scope
ShortList.cpp:24: error: 'e' was not declared in this scope
ShortList.cpp: At global scope:
ShortList.cpp:35: error: expected unqualified-id at end of input
我觉得自己好像在不折不扣地遵循C++教程。有人能解释一下我哪里错了吗?我知道扩展STL容器的功能是一种糟糕的形式,但这纯粹是一种学术追求。
我认为您的错误的直接原因是您需要typename
:
typename std::list<T>::iterator i, e = this.short_list.end();
因为编译器还没有意识到CCD_ 2是一个类型。
但您确实不想从std::list
派生,也不想编写自己的排序。
相关文章:
- 设计模式,以避免不必要地添加抽象函数以适应新功能
- 在大括号内添加语句会更改代码功能?
- 如何在数据库程序中添加"reduce the quantity of an item"功能?
- 如何在控制台应用程序中添加滚动功能以显示更大的输入
- C++ 添加编译器警告,以错误使用自定义打印/日志功能
- C++要在其中添加接口功能的类层次结构
- 在 C++ 中向多态树添加功能
- 如何将搜索功能添加到数组中
- 如何为内置类型添加成员功能,例如C 中的整数
- 疯狂(乘,添加,划分)哈希功能如何起作用
- 将功能参数添加到向量
- 使用指针在功能中的数组中添加/删除元素
- 将功能添加到默认复制构造函数中
- C 将类成员功能添加到地图中
- 如何将搜索功能添加到WXWIDGET C 中的TextCtrl
- 我应该如何将另一个类的方法的功能添加到我的当前类中?
- 为气泡排序功能添加计数器
- 无法为包装器功能添加完美的转发
- 当您下载其他库并将其功能添加到C ++程序中时,您是否能够在另一台PC上运行您的软件(没有库)?
- 如何在gcc中保留命令行选项,但为某些功能添加更多优化标志