Possible Duplicate:
length of array in function argument可能的重复:函数参数中数组的长度。
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
The only thing I am not sure of is if sizeof(v)/sizeof(int)
is the right way to go.
我唯一不确定的是sizeof(v)/sizeof(int)是否正确。
3 个解决方案
#1
9
sizeof(v)/sizeof(int)
is not the way to go. Your function is exactly equivalent to:
sizeof(v)/sizeof(int)不是一个好办法。你的函数完全等价于:
void permute(int *v, int curr, char *letters)
{
...
}
i.e. v
is not really an array, it's a pointer. You cannot pass arrays in C or C++.
也就是说,v不是一个数组,它是一个指针。不能通过C或c++中的数组。
The solution is one of the following (not exhaustive):
解决办法如下(并非详尽无遗)之一:
- add an extra argument that explicitly describes the length of the array
- 添加一个显式描述数组长度的额外参数。
- add an extra argument that points at the last element of the array
- 添加一个指向数组最后一个元素的额外参数
- use a proper container (e.g.
std::vector
), which you can callsize()
on - 使用合适的容器(例如std::vector),您可以将其称为size()。
- the template solution that @sehe suggests
- @sehe建议的模板解决方案
#2
5
One of my pet peeves: you can get C++ to deduce the array size for you
我最讨厌的一个问题是:您可以让c++为您推导数组大小
template <size_t N>
void permute(int (&v)[N], int curr,char letters[])
{
if(curr >= N)
{
checkit(v,letters);
}
for(int i = curr; i < N; i++)
{
swap(i,curr,v);
permute(v,curr + 1,letters);
swap(v[curr],v[i]);
}//for
}//permu
#3
0
In addition to Oli's answer: the typical way in C++ is to pass a pointer to the beginning and a pointer to the end of the sequence that you want to permute. By convention the beginning pointer is inclusive, the ending pointer is exclusive.
除了奥利的回答之外:c++中的典型方法是将指针传递到开头,并将指针传递到您想要执行的序列的末尾。按照惯例,开始指针是包含的,结束指针是独占的。
void permute(int *v, int *begin, int *end, char *letters) {
if (begin == end) {
checkit(v, end, letters);
} else {
...
permute(v, begin + 1, end, letters);
...
}
}
#1
9
sizeof(v)/sizeof(int)
is not the way to go. Your function is exactly equivalent to:
sizeof(v)/sizeof(int)不是一个好办法。你的函数完全等价于:
void permute(int *v, int curr, char *letters)
{
...
}
i.e. v
is not really an array, it's a pointer. You cannot pass arrays in C or C++.
也就是说,v不是一个数组,它是一个指针。不能通过C或c++中的数组。
The solution is one of the following (not exhaustive):
解决办法如下(并非详尽无遗)之一:
- add an extra argument that explicitly describes the length of the array
- 添加一个显式描述数组长度的额外参数。
- add an extra argument that points at the last element of the array
- 添加一个指向数组最后一个元素的额外参数
- use a proper container (e.g.
std::vector
), which you can callsize()
on - 使用合适的容器(例如std::vector),您可以将其称为size()。
- the template solution that @sehe suggests
- @sehe建议的模板解决方案
#2
5
One of my pet peeves: you can get C++ to deduce the array size for you
我最讨厌的一个问题是:您可以让c++为您推导数组大小
template <size_t N>
void permute(int (&v)[N], int curr,char letters[])
{
if(curr >= N)
{
checkit(v,letters);
}
for(int i = curr; i < N; i++)
{
swap(i,curr,v);
permute(v,curr + 1,letters);
swap(v[curr],v[i]);
}//for
}//permu
#3
0
In addition to Oli's answer: the typical way in C++ is to pass a pointer to the beginning and a pointer to the end of the sequence that you want to permute. By convention the beginning pointer is inclusive, the ending pointer is exclusive.
除了奥利的回答之外:c++中的典型方法是将指针传递到开头,并将指针传递到您想要执行的序列的末尾。按照惯例,开始指针是包含的,结束指针是独占的。
void permute(int *v, int *begin, int *end, char *letters) {
if (begin == end) {
checkit(v, end, letters);
} else {
...
permute(v, begin + 1, end, letters);
...
}
}