gpt4 book ai didi

c++ - 迭代器库中的std::size()在传递给函数的C样式数组上不起作用

转载 作者:行者123 更新时间:2023-12-01 15:12:50 26 4
gpt4 key购买 nike

#include <iostream>
#include <iterator>

void count(int foo[])
{
int dSize;
dSize=std::size(foo); //No matching function for call to 'size'
std::cout<<dSize<<std::endl;
}

int main()
{
int arr[5]={0,0,0,0,0};
count(arr);
return 0;
}

嗨,我正在尝试运行此代码,但它给了我这个错误。每当我不将数组传递给count函数时,std::size函数就可以正常工作,如下所示:
std::size(arr);// this work fine

是因为当您将其传递给函数时,数组会衰减到指针吗?

最佳答案

is it because of when you pass this to a function the array decay to a pointer?



几乎。更准确地说,这是因为您声明了函数参数为指针。 std::size仅适用于容器(或具有成员函数 size的任何对象)和数组。指针都不是。

关于c++ - 迭代器库中的std::size()在传递给函数的C样式数组上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62405407/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com