C++ 我可以得到std::array<;双自由度>*arr,其中dof将在运行时已知

C++ 我可以得到std::array<;双自由度>*arr,其中dof将在运行时已知,c++,arrays,C++,Arrays,出于某种原因,我需要以下任何一项才能工作: std::array*arr或std::array**arr 我需要从输入文件或命令行获取dof。或者,经过一些计算,我知道了dof的值 有解决办法吗?或者其他容器 (这是对现有代码和现有库的妥协。)我认为您不能。阅读答案了解更多信息 只需使用std::vector 现在,你有了这样的东西: #include <iostream> #include <vector> int main () { int dof, value

出于某种原因,我需要以下任何一项才能工作:

std::array*arr
std::array**arr

我需要从输入文件或命令行获取
dof
。或者,经过一些计算,我知道了
dof
的值

有解决办法吗?或者其他容器

(这是对现有代码和现有库的妥协。)

我认为您不能。阅读答案了解更多信息

只需使用std::vector

现在,你有了这样的东西:

#include <iostream>
#include <vector>

int main ()
{
  int dof, value;
  std::cout << "How many elements?\n"; // asume dof is one
  std::cin >> dof;
  std::cout << "Which value?\n"; // asume dof is one
  std::cin >> value;
  std::vector<int> v(dof, value);

  for(std::vector<int>::const_iterator it = v.begin();
      it != v.end(); ++it) {
    std::cout << *it << " ";
  }
  std::cout << std::endl;


  return 0;
}
#包括
#包括
int main()
{
int-dof,值;
std::cout>dof;
标准::cout>值;
标准::向量v(自由度,值);
对于(std::vector::const_迭代器it=v.begin();
it!=v.end();+it){

std::cout模板参数必须在编译时已知。您可以改用
std::vector

std::vector<double> arr(dof);

请使用反勾号
`
来格式化内联代码。否则,角括号
可能会被误认为是HTML。谢谢,但我需要固定大小对象的2d(或3d)数组。例如,它的工作方式如下。sruct{double x;double y}field;field**F;因此,我认为,向量arr(dof);但无法通过arr.data()获得;目的是以最小的更改概括代码。
auto* arr_ptr = arr.data();