3D 向量构造函数 C++

发布于 2024-12-08 13:13:57 字数 400 浏览 0 评论 0 原文

对于 2d 矢量,我知道我可以:

vector<vector<T>> vec;
vec = vector<vector<T>> (boardSize, vector<T>(boardSize));

但是对于 3d 矢量我该如何做呢?

我尝试过

vector<vector<vector<T>>> vec;
vec = vector<vector<vector<T>>> (boardSize, boardSize, vector<T>(boardSize));

但它无法编译。有什么想法吗?

For a 2d vector, I know I can go:

vector<vector<T>> vec;
vec = vector<vector<T>> (boardSize, vector<T>(boardSize));

But how do I do it for a 3d vector?

I tried

vector<vector<vector<T>>> vec;
vec = vector<vector<vector<T>>> (boardSize, boardSize, vector<T>(boardSize));

But it wouldn't compile. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原来是傀儡 2024-12-15 13:13:57

只是猜测:

vec = vector<vector<vector<T>>> (boardSize, vector<vector<T>>(boardSize, vector<T>(boardSize)));

这意味着,当您声明了 vector> 时,第二个参数应该是 vector;当您声明 vector>> 时,第二个参数应该是 vector>,反过来应该和第一种情况一样。

Just a guess:

vec = vector<vector<vector<T>>> (boardSize, vector<vector<T>>(boardSize, vector<T>(boardSize)));

That means, when you've declared a vector<vector<T>>, the second argument should be a vector<T>; and when you declared a vector<vector<vector<T>>>, the second argument should be a vector<vector<T>>, which in turn should be as in the first case.

丢了幸福的猪 2024-12-15 13:13:57

也许最好的办法就是不这样做。创建一个提供 3D 矢量接口的类(使用 一些更改)并在内部使用单维向量。

Probably the best thing would be not to do it. Create a class that offers the interface of a 3D vector (with some changes) and internally use a single dimension vector.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文