我正在尝试编写一个类来存储数百万个3D坐标数据.首先,我尝试使用3D数组来存储这些坐标数据.
#ifndef DUMPDATA_H
#define DUMPDATA_H
#define ATOMNUMBER 2121160
#include <string>
using namespace std;
class DumpData
{
public:
DumpData(string filename);
double m_atomCoords[ATOMNUMBER][3];
};
#endif // DUMPDATA_H
Run Code Online (Sandbox Code Playgroud)
然后我编译了程序,但是当我在ubuntu 14.04系统(64位)中运行程序时,我得到了段错误.所以我通过声明将3D数组更改为矢量:
vector < vector <double> > m_atomCoords;
Run Code Online (Sandbox Code Playgroud)
然后程序工作.我只是想知道在类中声明非常大的数组有限制吗?
在C++中,我可以做类似下面代码的事情吗?
在某个具体案例中,如果满足某些条件,是否可以跳转到另一个案例?
switch (tag) {
case 1: {
// do something
break;
}
case 2: {
if (/* condition */) {
// Can I somehow jump to case 1?
}
break;
}
}
Run Code Online (Sandbox Code Playgroud) 在 Windows MSVC 中,我试图编译以下代码。
void foo(std::vector<double> &bar){
const long int length = bar.size();
double a[length]; //error C3863: array type 'double [length]' is not assignable
for(int i=0; i < length; i++){
a[i]=0.0;
}
//do some other things
}
Run Code Online (Sandbox Code Playgroud)
代码在 xcode 中工作正常。
当我切换到 MSVC 时,使用命令行编译代码:
cl /EHsc main.cpp /std:c++17
Run Code Online (Sandbox Code Playgroud)
然后我有“错误C3863:数组类型'double [length]'不可分配”。
为什么不一样?如何摆脱错误?