【问题标题】:Array like struct of vectors [closed]像向量结构的数组[关闭]
【发布时间】:2015-01-21 15:59:09
【问题描述】:

我有一个关于数组以及如何自动调整数组大小的问题。 我看到向量会自动执行此操作,但向量仅适用于一维。 所以我认为struct 的向量可能会很好。

#include <vector>
struct 2dimensionarray
{ 
    vector<int> 1dimension;
    vector<int> 2dimension;
}

那么这是一个好方法吗?还是有更好的方法?

【问题讨论】:

  • 为什么不是向量的向量?
  • 尝试过的向量向量:vector &lt;vector&lt;int&gt;&gt; x?

标签: c++ arrays vector struct


【解决方案1】:

要创建一个二维向量,请将向量嵌套在模板部分中:

std::vector<std::vector<int>> 2dvector_name;

如果您需要多于两个维度,您可以做同样的事情:

std::vector<std::vector<std::vector<int>>> 3dvector_name;
std::vector<std::vector<std::vector<std::vector<int>>>> 4dvector_name;
etc...

【讨论】:

  • 但你知道数字作为变量名中的第一个字符在类 c 语言中是被禁止的...
  • 我进行了编辑以更清楚地说明名称只是一个占位符。我只是想确保他们了解向量的维度。
  • 感谢您的帮助。我也是菜鸟
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-07
  • 2018-04-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多