如何从类型列表中删除元素
how can I delete an element from a type-list?
我已经使用 c++ 中的模板实现了类似方案的整数列表:
class Empty {};
template <int H, class T = Empty>
struct list {
static const int head = H;
typedef T tail;
};
我正在尝试实现排序功能,所以我决定使用函数:删除和find_min。算法是:在列表中找到最小值,创建一个以这个最小值作为头部的列表,并将其从尾部(列表的其余部分)中删除。重要的是,我希望它以递归方式实现,没有任何循环。我的min_function :
template <class T,int N>
struct min_elem{};
template<int H, class T,int N>
struct min_elem<list<H,T>,N >{
static const int value = (N>H ? min_elem<T,H>::value : min_elem<T,N>::value );
};
template<int N>
struct min_elem<Empty,N>{
static const int value=N;
};
template <class T>
struct min{};
template <int I, class T>
struct min<list<I,T> > {
static const int value = min_elem<list<I,T>, I>::value;
};
但我不知道如何实现删除功能。我希望它类似于最小函数。谁能帮忙?功能的布局应该是:
template<class T, int K>
struct re{};
template<int I, class T, int K>
struct re <list<I,T>,K>{
typedef list< ?? don't know what should I exactly do here> value;
};
template<int I>
struct re <Empty,I>{
??
};
试试这种方式。
template<class T, int K> struct re;
template<int H, class T>
struct re <list<H, T>, H> { // the head and the subject are same value.
typedef T type;
};
template<int H, class T, int K>
struct re <list<H, T>, K>{
typedef list<H, typename re<T, K>::type> type;
};
template<int K>
struct re <Empty, K>{
typedef Empty type;
};
相关文章:
- 如何从存储在std::映射中的std::集中删除元素
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 从矢量中删除元素后出现隔离错误
- 如何使用remove_if从矢量中删除元素
- 如何从使用 for 循环中的矢量大小的矢量中删除元素
- C++映射不删除元素
- 如何从标准::元组中删除元素?
- 在动态数组中添加/删除C++元素
- 如何通过比较彼此的成员从QStringList中删除元素
- 如何修复从矢量中删除元素的错误?
- 从 std::set 中删除元素,同时在 C++17 中迭代该元素
- 使用remove_if从矢量中删除元素
- 用C++从三维矢量中删除元素
- 从对象数组中删除元素
- 如何在不使用 vector::erase() 的情况下编写自定义 Vector 方法来删除元素?
- C++ - 按自定义数据类型向量的值删除元素
- 从任意容器中廉价删除元素的惯用方法?
- 从数组中删除元素不起作用的函数
- 删除元素 BST
- 从向量中删除元素时未处理的异常