从文本文件中读取并使用 sizeof 结果计算整数的数量到 nan

Read from text file and count the number of integers using sizeof results to nan

本文关键字:整数 计算 nan 结果 sizeof 文件 文本 读取      更新时间:2023-10-16

我正在编写一个代码来计算从文本文件中读取的整数数,sizeof 函数显示"nan"。

double getSamplesize(vector<double> data)
{
    int samplesize;
    samplesize=sizeof(data);
    cout<<samplesize<< endl;
}

在 int(main( 上:

cout<<"sample size: " << getsamplesize(arr) << endl;

预期输出应为:样本数量:(数量(但是我得到了

(数字(样本量:南

double getSamplesize(vector<double> data)
^^^^^^

您已声明该函数返回 double 。但是您的函数不会以 return 语句结尾。因此,程序的行为是未定义的,而未定义的行为就是您观察到的。

sizeof(data)

这将返回变量类型的大小。它与元素的数量或向量拥有的数组的大小完全无关。

预期输出应为:样本量:(数量(但我得到

程序中流式传输到标准输出的第一段代码是以下行:

cout<<samplesize<< endl;

因此,预期的输出不可能以"样本大小:"开头。