我有这个代码:
class Grid {
public:
vector<vector<int> > grid;
int width;
int height;
Grid (int width, int height) width(width), height(height) {
...
}
};
Run Code Online (Sandbox Code Playgroud)
它使一个类称为Grid整数的2D数组.然而问题是,目前它只能是整数,但我想要它,所以它有点像std::vector你可以使用<>括号选择它将存储的类型的类.我的问题是,如何在我的班级中使用这些来替换所有当前的ints与任何其他类.
此外,你可能会说要查找它,但我尝试了,我找不到任何东西,可能是因为我不知道要搜索什么,所以如果有人能给我一个关于这甚至被称为什么的想法那么这将是有帮助的太.
您似乎只想模拟您的Grid课程:
template <typename T>
class Grid {
public:
vector<vector<T> > grid;
// initialize the vector with the correct dimensions:
Grid (int width, int height)
: grid(width, vector<double>(height)) {}
};
Run Code Online (Sandbox Code Playgroud)
然后实例化:
Grid<double> g(x, y);
Run Code Online (Sandbox Code Playgroud)
这将创建一个Grid地方的对象T是double