Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 初始化通用模板化容器_C++_Templates_Constructor_Operator Overloading - Fatal编程技术网

C++ 初始化通用模板化容器

C++ 初始化通用模板化容器,c++,templates,constructor,operator-overloading,C++,Templates,Constructor,Operator Overloading,我正在尝试编写一个通用容器(称为“tcontainer_t”),根据用户的意愿,它的内部实现可以使用vector或list以及t类型。当我尝试创建一个“tcontainer\u t”对象时,问题主要会出现——在运行时之前,没有人知道容器类型是vector还是list //tcontainer_t.h #include <vector> #include <list> using namespace std; template <class T, class Co

我正在尝试编写一个通用容器(称为“tcontainer_t”),根据用户的意愿,它的内部实现可以使用vector或list以及t类型。当我尝试创建一个“tcontainer\u t”对象时,问题主要会出现——在运行时之前,没有人知道容器类型是vector还是list

//tcontainer_t.h

#include <vector>
#include <list>
using namespace std;

template <class T, class Container >
class tContainer_t {
private:
    Container container;
    typedef typename Container::iterator iter_t;
    iter_t it;

public:
    tContainer_t();
    tContainer_t<T, Container>(const tContainer_t<T, Container>& other);
    tContainer_t<T, Container>& operator=(const tContainer_t<T, Container>& classObj);
    virtual ~tContainer_t();

};

#endif /* TCONTAINERT_H_ */
我知道这不是正确的方法

但我如何用这个来实例化向量或列表呢?构造函数中应该写什么


谢谢

如果您想要的只是一个默认构造的容器,那么您根本不需要写任何东西;编译器会为您执行此操作

如果要将容器初始化为不同的内容,可以使用构造函数初始值设定项。例如,假设
Container
有一个采用初始大小的构造函数(与标准容器一样),那么您可以编写

template<class T, class Container>
 tContainer_t<T, Container>::tContainer_t():
  container(25) // make a container with 25 elements
{
}
模板
t容器\u t::t容器\u t():
容器(25)//创建一个包含25个元素的容器
{
}

注意,在C++中,与java不同,成员变量<代码>容器< /> >是一个实际的对象类型:<代码>容器< /> >,而不仅仅是对一个的引用。code>newcontainer在堆上创建了一个容器,并返回指向该容器的指针。因此,您试图做的是将指向

容器
的指针分配给
容器
,这会失败,因为
容器
没有构造函数获取指向
容器
的指针


请注意,如果要取消引用由
new
返回的指针,它将进行编译(假设
Container
是可复制的,这对于标准容器仅意味着包含的类型是可复制的),但仍然不会执行您想要的操作:它将在堆上创建一个容器对象,将其分配给
Container
成员(这意味着将所有内容复制到对象<代码>容器<代码>),然后你会留下内存泄漏,因为<代码>新建<代码>返回的指针是临时的,没有分配,特别是没有删除。

首先,我觉得有必要指出以下几点

斯科特·迈尔斯,有效STL

第2项:小心容器独立代码的幻觉

STL是基于泛化的。数组被泛化为 容器,并根据其包含的对象类型进行参数化。 函数被泛化为算法,并在 它们使用的迭代器类型。指针被泛化为迭代器 并根据它们指向的对象类型进行参数化

这仅仅是开始。单个容器类型是通用的 分为序列容器和关联容器,并创建类似的容器 给定类似的功能。标准连续内存容器 (参见第1项)提供随机访问迭代器,而标准的基于节点的迭代器 容器(同样,请参见第1项)提供双向迭代器。 序列容器支持向前推和/或向后推,而 关联容器没有。关联容器提供 对数时间下限、上限和相等范围成员 函数,但序列容器没有

类模板

template <class T, class Container >
class tContainer_t {

int
int*
是完全不同的。

你是一名Java程序员,对吗?说真的,这门课的目标是什么?你想解决什么真正的问题?相信我,我不会发明这么糟糕的设计。这是我需要做的一项任务。你还应该指出,他试图将一个向量*分配给一个向量,因此编译器出现了错误。你知道,只是为了澄清他做错了什么:)我已经添加了关于这个的文本;谢谢你的建议。
this->container = new Container;  
template<class T, class Container>
 tContainer_t<T, Container>::tContainer_t():
  container(25) // make a container with 25 elements
{
}
template <class T, class Container >
class tContainer_t {
tContainer_t<int, vector<int*> > vContainer;