C++ C++;初始化类中的唯一\u ptr数组属性
我希望在这里为我的类使用一个唯一的_ptr(出于我自己的原因,而不是向量)。但我不知道如何用它初始化C++ C++;初始化类中的唯一\u ptr数组属性,c++,C++,我希望在这里为我的类使用一个唯一的_ptr(出于我自己的原因,而不是向量)。但我不知道如何用它初始化新[]数组。下面是我的代码 template <typename T> class kmap { private: const std::vector<std::string> &data; std::unique_ptr<T> table; public: kmap(std::vector<std::string> &k
新[]
数组。下面是我的代码
template <typename T>
class kmap
{
private:
const std::vector<std::string> &data;
std::unique_ptr<T> table;
public:
kmap(std::vector<std::string> &kmers);
};
template <typename T>
kmap<T>::kmap(std::vector<std::string> &kmers) : data(kmers)
{
this->table = std::unique_ptr<T[]>(new T[kmers.size()*2]);
}
编译方式:
g++-pipe-O2-std=c++14“$file”-o exe-lm
unique\u ptr
在使用数组类型声明时具有特殊性:
std::unique_ptr<T[]> table;
std::唯一的ptr表;
在这种情况下,delete[]
将用于删除指针
作为奖励,此专门化还定义了重载的
运算符[]
,以使其更易于用作数组。unique\u ptr
在使用数组类型声明时具有专门化:
std::unique_ptr<T[]> table;
std::唯一的ptr表;
在这种情况下,delete[]
将用于删除指针
作为奖励,此专门化还定义了一个重载的运算符[]
,以使其更易于用作数组。此std::unique_ptr表
与std::unique\u ptr(新…
的类型不同,您错过了表成员上的[]
。此std::unique\u ptr表
与std::unique\u ptr(new…
的类型不同,您错过了表成员上的[]
。