使用指向向量向量的唯一指针成员初始化模板化矩阵类
Initializing templated matrix class with unique pointer member to vector of vectors
我有
template<class T>
class Matrix{
public:
Matrix();
~Matrix();
private:
std::unique_ptr<std::vector<std::vector<T> > > PtrToMyMatrix;
};
我在初始化PtrToMyMatrix
时遇到困难。假设构造函数只应放入一个指向 T 的 1x1 矩阵PtrToMyMatrix
指针。我怎么写这个?
我想它是这样的
Matrix<T>::Matrix():PtrToMyMatrix(//Here it goes the value
){};
在值的位置,我想它应该像
new std::unique_ptr<vector<vector<T> > >(// Here a new matrix
)
代替新矩阵,我想它是这样的
new vector<vector<T> >(// Here a new vector
)
代替新矢量
new vector<T>(new T())
应该怎么样?
我认为你在追求这个:
std::unique_ptr<std::vector<std::vector<T> > >
PtrToMyMatrix(new std::vector<std::vector<T> >(1, std::vector<T>(1)));
构造函数std::vector(size_type n, const value_type& val);
(缺少分配(。
因此,您用1
构造的内vector
构造外vector
,该内用1
T
构造。
但是,很少需要动态创建std::vector
,因为它已经动态地存储其内部数据。您通常只需按值实例化它:
std::vector<std::vector<T> > MyMatrix(1, std::vector<T>(1));
相关文章:
- 为什么我在向量初始化时收到此错误?
- 使用向量初始化参数化构造函数的对象数组
- C++向量初始化遗传算法的代理向量
- shared_ptr类的向量的向量初始化问题
- 如何实现使用 gcc-4.4 编译的大向量初始化?
- 使用向量初始化C++中的链表
- C++ 中的向量初始化
- 如何使用嵌套初始化构造函数中的一维向量初始化矩阵
- std::另一个函数中的向量初始化
- 理解向量初始化时出现问题
- 静态常量 std::<char>没有堆的向量初始化?
- 向量初始化在 C++ 类中失败
- 具有从特定范围的随机数初始化特征矩阵或向量初始化特征矩阵或向量的有效方法
- 关于向量初始化的一些问题
- 了解C 中的向量初始化
- 设置::使用数字中的引号进行向量初始化
- VS2010中的向量初始化
- C++:构造函数中的向量初始化
- 如何从向量初始化数组?(如何将指针强制转换为数组?)
- 给我一个向量初始化的提示或(答案)