我可以得到std::array<double,dof>*arr,其中dof将已知运行时
can I get std::array<double, dof> *arr, where dof will be known runtime
由于某些原因,我需要以下任何一项才能工作:
std::array<double, dof> *arr;
或std::array<double, dof> **arr;
我需要从输入文件或命令行中获得dof
。或者,经过一些计算,我知道了dof
的值。
有变通办法吗?或者任何其他容器?
(这是对现有代码和现有库的妥协。)
模板参数必须在编译时已知。您可以使用std::vector
:
std::vector<double> arr(dof);
此外,在任何情况下都不需要指针。如果以后需要获取指向底层数组的指针,可以使用:
auto* arr_ptr = arr.data();
我想你做不到。阅读此答案了解更多信息。
只需使用std::矢量。
现在,你有这样的东西:
#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;
}
相关文章:
- arr[-1]在c++中的奇怪行为
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 在C++中用新表达式制作 arr[n][n]
- different between int **arr =new int [ n]; and int a[i][j]?
- arr[n] 是否以 C++ 为单位打印数组的长度?
- 为什么存储在数组中的地址值与其自己的地址相同,它应该指向某个不同的地址(&arr[0])?
- C++:' arr '和' arr[] '有什么区别
- 在 C/C++ 中将数组作为形式参数作为 int arr[] 和 int arr[N] 传递的区别
- 为什么 arr[i++] 与 arr[i]++ 会这样做?
- 这段代码中的arr[s[i]-'a']有什么用,为什么我们用"a"减去所有字符?
- 澄清了 strcpy() 在像这样初始化字符数组时的工作 *Arr.
- 如何理解"vector<int> avector (arr, arr + sizeof(arr) / sizeof(arr[0]) )"?
- 声明数组>> int arr[] 时出错;在C++(虽然在 Java 中有效)?
- 矢量<bool> arr 重置问题
- 根在 arr[0] 的二进制堆有什么好处
- arr[i] = i++ 和 i = i + 1 语句在 C 和 C++ 中的行为
- 访问函数中 const char *arr[] 的元素
- 数组问题:变量周围的堆栈'arr'已损坏
- 失败,dummy_array<int, 6> arr();
- 我可以得到std::array<double,dof>*arr,其中dof将已知运行时