【发布时间】:2022-01-24 12:25:00
【问题描述】:
我正在尝试查找此数组的长度,并希望找到它的长度,以便我可以在 for 循环中使用它。
// importing needed libraries
#include <iostream>
#include <string>
#include <cmath>
#include <set>
// preventing need of std::cout, std::endl etc
using namespace std;
int main()
{
int nums[] = {1, 2, 3, 4, 5, 6};
int size;
size = nums.size();
cout << size;
for (int i = 0; i < size; i++)
{
cout << nums[i] << endl;
}
return 0;
}
使用 .size() 方法的行是导致问题的行
问题:-
error: member reference base type 'int [6]' is not a structure or union
据我了解,我对 .size() 的使用应该是正确的,但显然不是。
【问题讨论】:
-
.size()是std::vector或std::array上的方法。普通 C++ 数组不支持方法调用。 -
您的理解从何而来?您可能误解了 C++ 教科书中的某些内容,您能引用教科书中的一段简短摘录让您相信这一点吗?
-
我刚在google上搜索过
标签: c++ arrays for-loop iterator sizeof