[英]Loop through array of unknown size C++
I am trying to traverse a double
array of an unknown size.我试图遍历一个未知大小的
double
数组。
Why does the following not work?为什么以下不起作用?
for (unsigned int = 1; i < sizeof(anArray)/sizeof(double); i++) {
...
}
Everything compiles fine ( g++ -Wall -Werror -std=c++11 app.cpp -o app
), but the program simply does not even enter the loop.一切都编译得很好(
g++ -Wall -Werror -std=c++11 app.cpp -o app
),但程序根本没有进入循环。
Full function:全功能:
struct stock_data {
int sell_index;
int buy_index;
double profit;
};
stock_data max_profit(double price_array[]) {
int sell_index = -1, buy_index = -1,
min = 0;
double profit = 0.0;
for(int i = 1; i < size; i++) {
if(price_array[i] - price_array[min] > profit) {
sell_index = min;
buy_index = i;
profit = price_array[i] - price_array[min];
}
if(price_array[i] < price_array[min]) {
min = i;
}
}
return {sell_index, buy_index, profit};
}
int main() {
double yesterday[] = {0.1, 0.2, 0.3, 0.4};
stock_data data = max_profit(yesterday);
cout << data.profit << endl;
}
In C++ operator sizeof is a compile-time operator.在 C++ 操作符中 sizeof 是一个编译时操作符。 That is its value is calculated by the compiler not at run-time.
也就是说它的值是由编译器计算的,而不是在运行时计算的。
So if as you say the array has unknown size then the compiler can not calculate the size of the memory occupied by the array.因此,如果您说数组的大小未知,那么编译器将无法计算数组占用的内存大小。
So it is evident that anArray is pointer to first element of the array that you might pass to the function.所以很明显 anArray 是指向你可能传递给函数的数组的第一个元素的指针。
Thus this expression因此这个表达式
sizeof(anArray)/sizeof(double)
is equivalent to expression相当于表达式
sizeof( double * )/sizeof( double ).
You have to pass also the size of the array explicitly to the function.您还必须将数组的大小显式传递给函数。
Take into account that an array passed by value to a function is converted to pointer to its first element.考虑到按值传递给函数的数组被转换为指向其第一个元素的指针。
Or the other approach is to use conteiner std::vector<double>
instead of the arrays.或者另一种方法是使用容器
std::vector<double>
而不是数组。
You could declare your function like你可以像这样声明你的函数
stock_data max_profit(double price_array[], size_t size );
and call it like并称之为
stock_data data = max_profit( yesterday,
sizeof( yesterday ) / sizeof( *yesterday ) );
sizeof(anArray)
returns the size of the type anArray
, probably *double
. sizeof(anArray)
返回类型anArray
的大小,可能是*double
。 You have to pass the array size as an additional parameter or use std::vector
and it's size
method.您必须将数组大小作为附加参数传递或使用
std::vector
及其size
方法。
Have a look at the C main function main(argc, argv)
where argv
are the parameters of the program and argc
is their count.看看 C 主函数
main(argc, argv)
其中argv
是程序的参数, argc
是它们的计数。 Or take strings, their end is determined by a special character \\0 that determines its end.或者以字符串为例,它们的结尾由一个特殊字符 \\0 确定,该字符决定了它的结尾。 But that's error prone.
但这很容易出错。
In C/C++ there is no other way with arrays.在 C/C++ 中,没有其他方法可以使用数组。
Your function max_profit
does not know the size of the array price_array
.您的函数
max_profit
不知道数组price_array
的大小。 If you want the function to accept arrays of varying size, you have two options:如果您希望函数接受不同大小的数组,您有两个选择:
size
value alongside the array (C style)size
值与数组一起传递(C 风格)std::vector
object which holds the size
inside (C++ style)size
std::vector
对象(C++ 风格)If you really want to avoid passing the size, you can add a NaN at the end of the array and check for it in the loop.如果你真的想避免传递大小,你可以在数组的末尾添加一个 NaN 并在循环中检查它。
#include<cmath>
...
for(int i = 0; !isnan(price_array[i]; i++){
...
}
To add the NaN when you're declaring the array:在声明数组时添加 NaN:
double price_array{0.0d, 5.9d, nan()};
Of course, this assumes you won't have any other NaNs in your array as part of the actual data.当然,这假设您的数组中没有任何其他 NaN 作为实际数据的一部分。
Instead of working out the size, you could just use:您可以使用:
for (auto x: anArray)
{
cout<<x<<endl;
}
This method loops through the entire array.此方法循环遍历整个数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.