C++ 模板和STL
以下代码表示基于std::vector的容器C++ 模板和STL,c++,templates,stl,C++,Templates,Stl,以下代码表示基于std::vector的容器 template <typename Item> struct TList { typedef std::vector <Item> Type; }; template <typename Item> class List { private typename TList <Item>::Type items; .... } int main() { L
template <typename Item>
struct TList
{
typedef std::vector <Item> Type;
};
template <typename Item>
class List
{
private
typename TList <Item>::Type items;
....
}
int main()
{
List <Object> list;
}
模板
结构列表
{
typedef std::向量类型;
};
模板
班级名单
{
私有的
typename-TList::类型项;
....
}
int main()
{
名单;
}
是否可以模板化std::vector并创建一个通用容器,类似这样的东西
template <typename Item, typename stl_container>
struct TList
{
typedef stl_container<Item>;
};
模板
结构列表
{
类型定义stl_容器;
};
其中stl_容器表示std::vector、std::list、std::set。。。?我想在创建时选择容器的类型
List <Object, std::vector> list; //vector of objects, not a real code
List <Object, std::vector> list; //list of objects, not a real code
列表//对象的向量,而不是真正的代码
名单//对象列表,而不是真正的代码
谢谢你的回答
更新问题:
我尝试了以下代码,但出现错误:
#include <vector>
template <typename Item, typename Container>
struct TList
{
typedef typename Container <Item>::type type; //Error C2059: syntax error : '<', Error C2238: unexpected token(s) preceding ';
};
template <typename T>
struct vector_container
{
typedef std::vector<T> type;
};
int _tmain(int argc, _TCHAR* argv[])
{
TList <int, vector_container> v;
TList <int, map_container> m;
}
#包括
模板
结构列表
{
typedef typename Container::type type;//错误C2059:语法错误:'是和否
您可以使用模板参数,例如:
template <typename Item, template <typename> class Container>
struct TList { /* ... */ };
但是,使用此模板,您将无法使用std::map
模板作为参数,因为它有四个模板参数:键和值类型、分配器类型和比较器类型
由于这种不灵活,通常更容易避免使用模板参数
是否可以模板化std::vector并创建一个通用容器,类似这样的东西
template <typename Item, typename stl_container>
struct TList
{
typedef stl_container<Item>;
};
不可以。您必须使用容器对函数或对象进行模板化——您不能对容器本身进行模板化
例如,考虑一个典型的<代码> STD::查找< /代码>:
template<class InputIterator, class T>
InputIterator find ( InputIterator first, InputIterator last, const T& value )
{
for ( ;first!=last; first++) if ( *first==value ) break;
return first;
}
模板
InputIterator查找(InputIterator first、InputIterator last、const T和value)
{
对于(;first!=last;first++)如果(*first==value)中断;
先返回;
}
这适用于任何容器,但根本不需要容器的tempalte
此外,考虑到您试图做的是创建容器独立代码,您可能想购买或借用第2项:当心容器独立代码的幻觉。好吧,您可以使用宏:
template <typename T, typename stl_container = std::vector<T> >
struct TList
{
typedef stl_container Type;
};
#define TLIST(T, C) TList<T, C<T> >
TList<int> foo;
TList<int, std::list<int> > bar;
TLIST(int, std::list) baz;
模板
结构列表
{
类型定义stl_容器类型;
};
#定义TLIST(T,C)TLIST
特利斯特福;
列表条;
TLIST(int,std::list)baz;
是,但不是直接:
template <typename Item, template <typename> class Container>
struct TList
{
typedef typename Container<Item>::type type;
};
template <typename Item, typename Container = std::vector<Item>>
struct TList
{};
这允许我们在给定分配器的情况下获得分配器
。如果没有此侵入性实用程序,我们如何对容器执行同样的操作?在C++0x中,很容易:
template <typename T, typename Container>
struct rebind; // not defined
template <typename T, typename Container, typename... Args>
struct rebind<T, Container<Args...>>
{
// assumes the rest are filled with defaults**
typedef Container<T> type;
};
模板
struct rebind;//未定义
模板
结构重新绑定
{
//假设其余的都是默认值**
类型定义容器类型;
};
例如,给定std::vector
,我们可以执行rebind::type
。与以前的C++0x解决方案不同,此解决方案可以在C++03中通过宏和迭代进行模拟
**请注意,这种机制可以变得更加强大,比如指定哪些参数要保留,哪些参数要重新绑定,哪些参数在用作参数之前要重新绑定,等等,但这只是留给读者的练习。我有点困惑为什么一些非常聪明(和有能力)的人会说不
除非我误解了您的问题,否则您试图完成的工作实际上与标准库中的“容器适配器”完全相同。每个适配器都提供了一个到某个底层容器类型的接口,其中容器类型将用作模板参数(具有默认值)
例如,std::stack
使用其他容器(例如,std::deque
,std::list
或std::vector
)保存对象,而std::stack
本身只为您只想使用堆栈操作时提供了一个简化/受限的接口。std::stack
将使用的底层容器作为模板参数提供。以下是标准中的代码外观:
namespace std {
template <class T, class Container = deque<T> >
class stack {
public:
typedef typename Container::value_type value_type;
typedef typename Container::size_type size_type;
typedef Container container_type;
protected:
Container c;
public:
explicit stack(const Container& = Container());
bool empty() const { return c.empty(); }
size_type size() const { return c.size(); }
value_type& top() { return c.back(); }
const value_type& top() const { return c.back(); }
void push(const value_type& x) { c.push_back(x); }
void pop() { c.pop_back(); }
};
}
名称空间std{
模板
类堆栈{
公众:
typedef typename容器::值\类型值\类型;
typedef typename容器::大小\类型大小\类型;
类型定义容器容器类型;
受保护的:
容器c;
公众:
显式堆栈(const Container&=Container());
bool empty()常量{返回c.empty();}
size_type size()常量{返回c.size();}
value_type&top(){return c.back();}
const value_type&top()const{return c.back();}
void push(const value_type&x){c.push_back(x);}
void pop(){c.pop_back();}
};
}
当然,也许我只是误解了这个问题——如果是这样,我会提前向我(某种程度上)认识的人道歉不同意。您可以像其他人在这里提到的那样使用模板参数。这方面的主要困难不是不同的容器类型具有不同的模板参数,而是标准允许标准容器(如vector)除了有文档记录的必要参数外,还具有模板参数
您可以通过提供自己的子类类型来解决这个问题,这些子类类型接受适当的模板参数,并允许在我的实现中填充任何额外的(必须具有默认值):
template < typename T > struct simple_vector : std::vector<T> {};
templatestruct simple_vector:std::vector{};
也可以使用模板化的typedef习惯用法:
template < typename T > struct retrieve_vector { typedef std::vector<T> type; };
templatestruct retrieve_vector{typedef std::vector type;};
谁会使用这个类?为什么不能列出
只使用类型定义向量/列表/集合项
?给定一个容器类型和一个值类型,一个简单地将这两种类型放在一起的类的用途是什么?关于:编辑,仍然看不出为什么不能编写列表;
等可以替换模板类s容器
仅包含类型名Contai
namespace std {
template <class T, class Container = deque<T> >
class stack {
public:
typedef typename Container::value_type value_type;
typedef typename Container::size_type size_type;
typedef Container container_type;
protected:
Container c;
public:
explicit stack(const Container& = Container());
bool empty() const { return c.empty(); }
size_type size() const { return c.size(); }
value_type& top() { return c.back(); }
const value_type& top() const { return c.back(); }
void push(const value_type& x) { c.push_back(x); }
void pop() { c.pop_back(); }
};
}
template < typename T > struct simple_vector : std::vector<T> {};
template < typename T > struct retrieve_vector { typedef std::vector<T> type; };