可能的重复:
函数参数中数组的长度
我正在做作业,我完全被难住了.我们假设列表的每个顺序都是一个整数数组,所以我根据老师的伪代码编写了这段代码:
Hi am doing homework and I am completly stumped. We were suppose to get every order of a list an array of integers so I wrote this piece of code, based off of my teacher's pseudocode:
void permute(int v[], int curr,char letters[])
{
if(curr >= sizeof(v)/sizeof(int))
{
checkit(v,letters);
}
for(int i = curr; i < sizeof(v)/sizeof(int); i++)
{
swap(i,curr,v);
permute(v,curr + 1,letters);
swap(v[curr],v[i]);
}//for
}//permu
我唯一不确定的是 sizeof(v)/sizeof(int) 是否正确.
The only thing I am not sure of is if sizeof(v)/sizeof(int) is the right way to go.
sizeof(v)/sizeof(int) 不是要走的路.您的函数完全等同于:
sizeof(v)/sizeof(int) is not the way to go. Your function is exactly equivalent to:
void permute(int *v, int curr, char *letters)
{
...
}
即v 不是真正的数组,它是一个指针.你不能在 C 或 C++ 中传递数组.
i.e. v is not really an array, it's a pointer. You cannot pass arrays in C or C++.
解决方案是以下之一(并非详尽无遗):
The solution is one of the following (not exhaustive):
std::vector),您可以在其上调用 size()std::vector), which you can call size() on这篇关于作为函数参数传递的数组的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
当没有异常时,C++ 异常会以何种方式减慢代码速In what ways do C++ exceptions slow down code when there are no exceptions thown?(当没有异常时,C++ 异常会以何种方式减慢代码速度?)
为什么要捕获异常作为对 const 的引用?Why catch an exception as reference-to-const?(为什么要捕获异常作为对 const 的引用?)
我应该何时以及如何使用异常处理?When and how should I use exception handling?(我应该何时以及如何使用异常处理?)
C++中异常对象的范围Scope of exception object in C++(C++中异常对象的范围)
从构造函数的初始化列表中捕获异常Catching exceptions from a constructor#39;s initializer list(从构造函数的初始化列表中捕获异常)
C++03 throw() 说明符 C++11 noexcept 之间的区别Difference between C++03 throw() specifier C++11 noexcept(C++03 throw() 说明符 C++11 noexcept 之间的区别)