找不到列表中的最后一个元素 [英] Can't find last element in list
问题描述
如何找到下面定义的列表numsx中的最后一个元素?
int * myfind(int * arr_start,int * arr_end,int& s){
int not_found = 666;
int * result =& not_found;
while(arr_start!= arr_end){
if(*(arr_start)== s)
{
result = arr_start;
}
>
arr_start ++;
}
返回结果;
}
int main(){
int numsx [] = {1,2,3,4,5,7,8,9};
int * resultx;
int endx =(sizeof(numsx)/ 4)-1;
int sx = 9;
resultx = :: myfind(numsx,numsx + endx,sx);
std :: cout<< * resultx<< std :: endl;
返回0;
}
我想过< b> :
while(arr_start!= arr_end + 1){
....
。 ...
代替,但是因为arr_end + 1是不知名的领域我觉得它的风格很糟糕。
有没有办法确保列表中的最后一个元素被读取
并且只能在之后终止?
How do I find the last element in the list numsx defined below?
int* myfind(int* arr_start, int* arr_end, int& s) {
int not_found = 666;
int* result = ¬_found;
while (arr_start != arr_end){
if (*(arr_start) == s)
{
result = arr_start;
}
arr_start++;
}
return result;
}
int main() {
int numsx[] = { 1, 2, 3, 4, 5, 7, 8, 9};
int* resultx;
int endx = (sizeof(numsx)/4)-1;
int sx = 9;
resultx = ::myfind( numsx, numsx + endx,sx);
std::cout << *resultx << std::endl;
return 0;
}
I have thought about:
while (arr_start != arr_end+1){
....
....
instead, but since "arr_end+1" is unknow territory I guess its bad style.
Is there someway to make sure that the last element in a list is read
and only terminate afterwards?
推荐答案
desktop写道:
desktop wrote:
如何找到列表中的最后一个元素numsx定义如下?
int * myfind(int * arr_start,int * arr_end,int& s){
How do I find the last element in the list numsx defined below?
int* myfind(int* arr_start, int* arr_end, int& s) {
为什么是''s'通过
Why is ''s'' passed by
int not_found = 666;
int * result =& not_found;
while(arr_start!= arr_end){
if(*(arr_start)== s)
{
结果= arr_start;
}
arr_start ++;
}
返回结果;
int not_found = 666;
int* result = ¬_found;
while (arr_start != arr_end){
if (*(arr_start) == s)
{
result = arr_start;
}
arr_start++;
}
return result;
杂乱,杂乱......鉴于界面(忘记666)你需要
来实现这个
while(arr_start!= arr_end){
if(* arr_start == s)
break;
++ arr_start ;
}
返回arr_start;
Clutter, clutter... Given the interface (forget the 666) you
should implement this as
while (arr_start != arr_end) {
if (*arr_start == s)
break;
++arr_start;
}
return arr_start;
}
int main(){
int numsx [] = {1,2,3,4,5,7,8,9};
int * resultx;
int endx =(sizeof(numsx)/ 4)-1;
}
int main() {
int numsx[] = { 1, 2, 3, 4, 5, 7, 8, 9};
int* resultx;
int endx = (sizeof(numsx)/4)-1;
两个错误。首先,不要使用4,使用''sizeof(int)''。第二,
如果你不减去,你会好得多。使用范围
,其中上限值不包括(这称为开放的
范围)。 IOW
int endx = sizeof(numsx)/ sizeof(numsx [0]);
Two mistakes. First, don''t use 4, use ''sizeof(int)''. Second,
you''re much better off if you don''t subtract 1. Use the range
where the upper value is not includede (this is called "an open
range"). IOW
int endx = sizeof(numsx) / sizeof(numsx[0]);
int sx = 9;
resultx = :: myfind(numsx,numsx + endx,sx);
int sx = 9;
resultx = ::myfind( numsx, numsx + endx,sx);
因为''resultx''是一个指针,它可能等于''numsx + endx'',
这意味着未找到。
Since ''resultx'' is a pointer, it could be equal to ''numsx + endx'',
which will mean "not found".
>
std :: cout<< * resultx<< std :: endl;
返回0;
}
我想过< b> :
while(arr_start!= arr_end + 1){
...
.. 。
而不是,因为arr_end + 1是不知名的领域我猜它不好
风格。
>
std::cout << *resultx << std::endl;
return 0;
}
I have thought about:
while (arr_start != arr_end+1){
...
...
instead, but since "arr_end+1" is unknow territory I guess its bad
style.
不,它实际上更好。
No, it''s actually better.
>
是否有某种方法可以确保列表中的最后一个元素被读取
并且只能在之后终止?
>
Is there someway to make sure that the last element in a list is read
and only terminate afterwards?
见上文。
V
-
请在通过电子邮件回复时删除大写''A'
我没有回复最热门的回复,请不要问
See above.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
>
Victor Bazarov写道:
Victor Bazarov wrote:
desktop写道:
desktop wrote:
>我如何找到下面定义的列表numsx中的最后一个元素?
int * myfind(int * arr_start,int * arr_end,int& s){
>How do I find the last element in the list numsx defined below?
int* myfind(int* arr_start, int* arr_end, int& s) {
为什么''s'通过
Why is ''s'' passed by
> int not_found = 666;
int * result =& not_found;
while(arr_start!= arr_end){
if(*(arr_start)== s)
{
result = arr_start;
}
arr_start ++;
}
返回结果;
>int not_found = 666;
int* result = ¬_found;
while (arr_start != arr_end){
if (*(arr_start) == s)
{
result = arr_start;
}
arr_start++;
}
return result;
杂乱,杂乱......鉴于界面(忘记666)你需要实现这一点
$ b / b
>
while(arr_start!= arr_end){
if(* arr_start == s)
break;
++ arr_start ;
}
返回arr_start;
Clutter, clutter... Given the interface (forget the 666) you
should implement this as
while (arr_start != arr_end) {
if (*arr_start == s)
break;
++arr_start;
}
return arr_start;
>}
>}
问题是我不能使用''break''。我将使用一个
自定义优化器,它不允许使用''break''关键字。
Problem is that I am not allowed to use ''break''. I am going to use a
custom made optimizer that will not allow the use of the ''break'' keyword.
desktop写道:
desktop wrote:
Victor Bazarov写道:
Victor Bazarov wrote:
> desktop写道:
>desktop wrote:
>>如何找到下面定义的列表numsx中的最后一个元素?
int * myfind(int * arr_start,int * arr_end,int& s){
>>How do I find the last element in the list numsx defined below?
int* myfind(int* arr_start, int* arr_end, int& s) {
为什么''s''通过
Why is ''s'' passed by
>> int not_found = 666;
int * result =& not_found;
while(arr_start!= arr_end){
if(*(arr_start)== s)
{
result = arr_start ;
}
返回结果;
>>int not_found = 666;
int* result = ¬_found;
while (arr_start != arr_end){
if (*(arr_start) == s)
{
result = arr_start;
}
arr_start++;
}
return result;
杂乱,杂乱......鉴于界面(忘了666)你应该实现这个(
)(arr_start!= arr_end) {
if(* arr_start == s)
break;
++ arr_start;
}
返回arr_start;
Clutter, clutter... Given the interface (forget the 666) you
should implement this as
while (arr_start != arr_end) {
if (*arr_start == s)
break;
++arr_start;
}
return arr_start;
>>}
>>}
问题是我不能使用''break''。我将使用一个
自定义优化器,它不允许使用''break''
关键字。
Problem is that I am not allowed to use ''break''. I am going to use a
custom made optimizer that will not allow the use of the ''break''
keyword.
哦,来吧!然后使用''return''。
V
-
请删除资金''A'时通过电子邮件回复
我没有回复最热门的回复,请不要问
Oh, come on! Use ''return'', then.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
这篇关于找不到列表中的最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!