如何在 C++ 中使用 sizeof() 进行reference_to_array [duplicate]

how to use sizeof() for reference_to_array in c++ [duplicate]

提问人:semicolon_missing 提问时间:8/19/2023 最后编辑:Remy Lebeausemicolon_missing 更新时间:8/20/2023 访问量:82

问:

如何使用来确定对数组的引用的大小?sizeof()

我已经声明了一个数组,并用于打印它所占用的总大小。main()sizeof()

然后,我将数组作为对数组的引用传递给函数,但我无法用于引用(数组)变量。sizeof()

#include <iostream>

double sum(double (&list)[], size_t size);

int main(){
    double arr[]{34.5, 67.8, 92.345, 8.1234, 12.314, 3.4};
    double result{};

    std::cout << "size of the array : " << sizeof(arr) << std::endl;
    result = sum(arr, std::size(arr));
    std::cout << "the total is " << result << std::endl;
    
    return 0;
}

double sum(double (&list)[], size_t size){
    double total{};
    std::cout << "the size is : " << sizeof(list) << std::endl;

    for( int i{} ; i < size ; i++){
        total += list[i];
    }
    
    return total;
}

sizeof(list)显示编译器错误:

error: invalid application of ‘sizeof’ to incomplete type ‘double []’
     std::cout << "the size is : " << sizeof(list) << std::endl;

在我得到我想要的输出时更改函数参数后,但是为什么没有像声明时那样工作,而没有在声明中明确提及其大小,尽管它是一个参考?double (&list)[6]sizeof(list)sizeof(arr)list

C++ 数组 按引用传递

评论

3赞 Eljay 8/19/2023
如何使用 sizeof() 确定对数组的引用的大小。你没有。那不是目的。您需要将数组的大小作为参数传递。或者使用 .或者使用 .或者使用模板技术来引用数组。或者使用 C++ 惯用的开始和结束作为函数的迭代器参数,从中可以计算大小。sizeofstd::arraystd::vector
3赞 Brian61354270 8/19/2023
正确的 C++ 答案是不要使用 C 样式数组。使用、、 或迭代器(如果适用)。请注意,使用 并不是此代码的唯一问题。如果你“修复”它,你仍然会得到一个“从'double[6]'到'double(&)[]'的未知转换”错误std::arraystd::vectorstd::spansizeof
0赞 Brian61354270 8/19/2023
回复:“但是为什么 sizeof(list) 不像 sizeof(arr) 那样工作”,请参阅在函数中查找数组的长度和如何找到数组的长度? 以及 当一个函数有一个特定大小的数组参数时,为什么它被替换为指针?
1赞 Pete Becker 8/19/2023
在 中,打印出 sizeof(arr)' 的值。他们做两件不同的事情。mainstd::size(arr), and compare it with
1赞 Remy Lebeau 8/19/2023
不能创建对未知大小的数组的引用。如果希望函数接受任何大小的数组,则需要使用模板 () 或指针 () 而不是引用。template <size_t N> double sum(double (&list)[N]);double sum(double *list, size_t size);

答:

2赞 machine_1 8/19/2023 #1

C 样式数组的大小是其类型的一部分,因此声明为的数组具有完整类型。double arr[6];(double)[6].

运算符在编译时工作。在数组上使用时,它会返回数组的大小(以字节为单位)。这是可能的,因为数组的大小是其类型的一部分,并且编译器在编译时就知道它。sizeofsizeof

但是,如果您尝试在不完整的类型(如 )上使用,这将导致编译错误,因为参数的类型必须是完整的。sizeof(double)[]sizeof

1赞 Pepijn Kramer 8/20/2023 #2

使用 C++ 我希望这样的代码没有任何大小:

#include <iostream>
#include <numeric>
#include <vector>

double sum(const std::vector<double>& values)
{
    double sum{ 0.0 };

    // IF you want to write loops, prefer using range based for loops (they will 'know' the size of values)
    for (const auto value : values)
    {
        sum += value;
    }

    return sum;
}

int main() 
{
    std::vector<double> values{ 34.5, 67.8, 92.345, 8.1234, 12.314, 3.4 };
    double result = sum(values);

    // no need to use sizeof in current C++, just ask the vector
    std::cout << "size of the array : " << values.size() << "\n";
    std::cout << "the total is " << result << "\n";

    // or to write code with no loops (in your code) at all
    std::cout << "the total using accumulate is " << std::accumulate(values.begin(), values.end(), 0.0);

    return 0;
}