简体   繁体   English

从文本文件读取并使用sizeof结果将整数计数为nan

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

I'm writing a code to count the number of integers read from a text file, sizeof function is displaying "nan". 我正在编写代码以计算从文本文件读取的整数数量,sizeof函数显示“ nan”。

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

}

on the int(main): 在int(main)上:

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

Expected output should be: sample size: (number) But im getting 预期输出应为:样本数量:(数量),但即时

(number) sample size: nan (数量)样本数量:nan

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

You've declared that the function returns double . 您已经声明该函数返回double But your function does not end in a return statement. 但是您的函数不会以return语句结尾。 As a result, the behaviour of the program is undefined, and that undefined behaviour is what you observe. 结果,该程序的行为是不确定的,而未定义的行为就是您观察到的。

 sizeof(data) 

This returns the size of the type of the variable. 这将返回变量类型的大小。 It is completely unrelated to the number of elements, or the size of the array owned by the vector. 它与元素的数量或向量所拥有的数组的大小完全无关。

Expected output should be: sample size: (number) But im getting 预期输出应为:样本数量:(数量),但即时

The first piece of code in your program that streams into standard output is this line: 程序中流到标准输出的第一段代码是以下行:

 cout<<samplesize<< endl; 

Thus the expected output cannot possibly begin with "sample size:". 因此,预期的输出可能无法以“样本大小:”开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM