![](/img/trans.png)
[英]Using an array of counters extract the integers from a file and count the number of responses?
[英]Read from text file and count the number of integers using sizeof results to nan
我正在編寫代碼以計算從文本文件讀取的整數數量,sizeof函數顯示“ nan”。
double getSamplesize(vector<double> data)
{
int samplesize;
samplesize=sizeof(data);
cout<<samplesize<< endl;
}
在int(main)上:
cout<<"sample size: " << getsamplesize(arr) << endl;
預期輸出應為:樣本數量:(數量),但即時
(數量)樣本數量:nan
double getSamplesize(vector<double> data) ^^^^^^
您已經聲明該函數返回double
。 但是您的函數不會以return
語句結尾。 結果,該程序的行為是不確定的,而未定義的行為就是您觀察到的。
sizeof(data)
這將返回變量類型的大小。 它與元素的數量或向量所擁有的數組的大小完全無關。
預期輸出應為:樣本數量:(數量),但即時
程序中流到標准輸出的第一段代碼是以下行:
cout<<samplesize<< endl;
因此,預期的輸出可能無法以“樣本大小:”開頭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.