为什么会出现错误:size不是std的成员

why is there an error: size is not a member of std?

本文关键字:不是 std 成员 size 错误 为什么      更新时间:2023-10-16
#include <iostream> // for std::cout
#include <iterator> // for std::size
int main()
{
int array[] = { 1, 1, 2, 3, 5, 8, 13, 21 };
std::cout << "The array has: " << std::size(array) << " elementsn";
return 0;
}

当我运行这段代码时,我得到了类似size不是std成员的信息。任何评论都将不胜感激。

编译器不支持C++17,或者编译器选项没有激活这种支持

在任何情况下,您都可以使用标头<type-traits>中声明的标准类模板std::extent。例如

#include <iostream> // for std::cout
#include <type_traits> // for std::extent
int main()
{
int array[] = { 1, 1, 2, 3, 5, 8, 13, 21 };
std::cout << "The array has: " << std::extent<decltype( array )>::value << " elementsn";
return 0;
}