So I have a file with three columns in e.g:
所以我有一个包含三列的文件,例如:
1 1 750
These are x,y,intensity values.
这些是x,y,强度值。
Then I try to read in this long file into arrays. This is the code so far:
然后我尝试将这个长文件读入数组。这是到目前为止的代码:
using std::cout;
using std::endl;
using std::string;
string fOutFileName("gaintest.root");
int main()
{
std::ifstream file1("data_p30.dat");
double intensity;
int i;
int j ;
double c[1000][1000];
if (file1.is_open()) {
file1.seekg(0);
while (!file1.eof()) {
file1 >> i >> j >> intensity;
c[i][j]=intensity;
cout<<c[i][j]<<'/n';
}
file1.close();
} else cout << "Error, cannot open file 1";
}
So ultimately I want to be able to have a 2D array that is linked to the intensity. Any ideas on why I am failing? It compiles fine but when it run, it looks like this:
所以最终我希望能够拥有一个与强度相关的2D数组。关于我为什么失败的任何想法?它编译得很好但是当它运行时,它看起来像这样:
root [0]
*** Break *** segmentation violation
===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0 0x0000003701098c05 in waitpid () from /lib64/libc.so.6
#1 0x000000370103c481 in do_system () from /lib64/libc.so.6
#2 0x00002b036f5ebc6a in TUnixSystem::StackTrace() ()
from /batchsoft/root/root528-x86_64-slc5-43/lib/libCore.so
#3 0x00002b036f5eb63c in TUnixSystem::DispatchSignals(ESignals) ()
from /batchsoft/root/root528-x86_64-slc5-43/lib/libCore.so
#4 <signal handler called>
#5 0x00002b0370acd515 in TRint::Run(bool) ()
from /batchsoft/root/root528-x86_64-slc5-43/lib/libRint.so
#6 0x000000000040106d in main ()
===========================================================
The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#5 0x00002b0370acd515 in TRint::Run(bool) ()
from /batchsoft/root/root528-x86_64-slc5-43/lib/libRint.so
#6 0x000000000040106d in main ()
===========================================================
2 个解决方案
#1
2
In C++
double c[1000, 1000];
The first 1000 is thrown away and the compiler generates code for:
抛弃前1000个,编译器生成以下代码:
double c[1000];
What you wrote [1000, 1000] is a multi-dimensional array in Pascal, but not in C++. In C++ you would use:
您编写的内容[1000,1000]是Pascal中的多维数组,但不是C ++中的数组。在C ++中,您将使用:
double c[1000][1000];
#2
0
The errors you have displayed are generated by ROOT. Since this is not a ROOT forum, ROOT is a c++ data analysis framework developed by scientists at CERN. For the curious, their website is here. Your problem however, is not related to ROOT.
您显示的错误由ROOT生成。由于这不是ROOT论坛,ROOT是由CERN的科学家开发的c ++数据分析框架。对于好奇,他们的网站就在这里。但是,您的问题与ROOT无关。
2D arrays are declared by
2D数组由声明
double a[2][2];
and this will work for you.
这对你有用。
Perhaps more safe is to use the template class
也许更安全的是使用模板类
std::vector<double> v;
For a 2D application this would look like
对于2D应用程序,这看起来像
std::vector<std::vector<double> > v2;
The advantage of this is that its size can be adjusted as needed and
这样做的好处是可以根据需要调整其大小
v.push_back(d);
will add an element to the end of the vector, elongating it if necessary. Elements can also be accessed with array syntax like
将向量的末尾添加一个元素,必要时将其拉长。也可以使用数组语法访问元素
v[1]
or
v2[1][2]
#1
2
In C++
double c[1000, 1000];
The first 1000 is thrown away and the compiler generates code for:
抛弃前1000个,编译器生成以下代码:
double c[1000];
What you wrote [1000, 1000] is a multi-dimensional array in Pascal, but not in C++. In C++ you would use:
您编写的内容[1000,1000]是Pascal中的多维数组,但不是C ++中的数组。在C ++中,您将使用:
double c[1000][1000];
#2
0
The errors you have displayed are generated by ROOT. Since this is not a ROOT forum, ROOT is a c++ data analysis framework developed by scientists at CERN. For the curious, their website is here. Your problem however, is not related to ROOT.
您显示的错误由ROOT生成。由于这不是ROOT论坛,ROOT是由CERN的科学家开发的c ++数据分析框架。对于好奇,他们的网站就在这里。但是,您的问题与ROOT无关。
2D arrays are declared by
2D数组由声明
double a[2][2];
and this will work for you.
这对你有用。
Perhaps more safe is to use the template class
也许更安全的是使用模板类
std::vector<double> v;
For a 2D application this would look like
对于2D应用程序,这看起来像
std::vector<std::vector<double> > v2;
The advantage of this is that its size can be adjusted as needed and
这样做的好处是可以根据需要调整其大小
v.push_back(d);
will add an element to the end of the vector, elongating it if necessary. Elements can also be accessed with array syntax like
将向量的末尾添加一个元素,必要时将其拉长。也可以使用数组语法访问元素
v[1]
or
v2[1][2]