【发布时间】:2021-06-24 12:45:38
【问题描述】:
我一直被这个问题困扰。这听起来可能很荒谬,但这正是我的终端发生的事情。
int* nums = new int[100];
cout << sizeof(nums);
但是上面的块输出8而不是100;有人可以帮忙吗?
完整程序(readfile.cpp):
// imports
pair<int, int*> Readfile::readfile (string dirc) {
string fn;
if (dirc.compare("") == 0) {
cout << ">>> Input file name: "; cin >> fn; cout << endl;
}
else
fn = dirc;
ifstream infile(fn);
string line;
vector<int> ints;
while (getline(infile, line)) {
istringstream iss(line);
for (string k; iss >> k; )
ints.push_back(stoi(k));
}
int* nums = new int[100];
cout << sizeof(nums);
copy(ints.begin(), ints.end(), nums);
int n = sizeof(nums)/sizeof(nums[0]);
pair<int, int*> pii(n, nums);
return pii;
}
int main() {
Readfile rdf;
rdf.readfile("..\\insertion\\temp.txt"); // a text file consisting of 100 random integers separated by spaces
return 0;
}
非常感谢任何帮助。
【问题讨论】:
-
老实说,我真的很想得到一个答案,除了“不”;我之前已经完成将值从 int 向量复制到 int 数组,但不知何故这次它不起作用
-
@crimsonpython24 -- 指针除了指向单个实体外,什么都不知道。它不知道它指向的内容后面有多个项目。问题是你被引导相信指针知道一些信息,而不是它指向的单个元素。