这是有效的代码吗? [英] Is this valid code?

查看:76
本文介绍了这是有效的代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正和朋友讨论以下是否有效

代码。我很确定这是不允许的,但我的朋友似乎认为

没关系。它绝对可以在我们可以获得的所有编译器中运行良好。有什么意见吗?


Bob


void remove_duplicates(std :: list< int>& in_list)

{

std :: list< int> :: iterator it = in_list.begin();

in_list.remove(* it);

}

I was having a discussion with a friend about if the following is valid
code. I''m fairly sure it isn''t allowed, but my friend seems to think
it''s fine. It definatly appears to run fine in all the compilers we can
get access to. Any comments?

Bob

void remove_duplicates(std::list<int>& in_list)
{
std::list<int>::iterator it = in_list.begin();
in_list.remove(*it);
}

推荐答案

Bob Brian写道:
Bob Brian wrote:
我和朋友讨论过以下是否有效<代码。我很确定这是不允许的,但我的朋友似乎认为这很好。它绝对可以在我们可以访问的所有编译器中运行良好。任何评论?

Bob

void remove_duplicates(std :: list< int>& in_list)
{
std :: list< int> :: iterator it = in_list.begin();
in_list.remove(* it);
}
I was having a discussion with a friend about if the following is valid
code. I''m fairly sure it isn''t allowed, but my friend seems to think
it''s fine. It definatly appears to run fine in all the compilers we can
get access to. Any comments?

Bob

void remove_duplicates(std::list<int>& in_list)
{
std::list<int>::iterator it = in_list.begin();
in_list.remove(*it);
}




嗯,为什么你认为它不会编译?


我认为它会编译,但它不会做它应该做的。

它的作用是删除in_list中等于*(in_list.begin())的值

的所有元素。


除此之外,如果in_list为空,你的函数的行为很可能是未定义的。


问候,

Matthias


matthias_k写道:
matthias_k wrote:
Bob Brian写道:
Bob Brian wrote:
我正和朋友讨论是否以下是
有效代码。我很确定这是不允许的,但我的朋友似乎认为这很好。它绝对可以在所有编译器中运行良好
我们可以访问。任何评论?

Bob

void remove_duplicates(std :: list< int>& in_list)
{
std :: list< int> :: iterator it = in_list.begin();
in_list.remove(* it);
}
I was having a discussion with a friend about if the following is
valid code. I''m fairly sure it isn''t allowed, but my friend seems to
think it''s fine. It definatly appears to run fine in all the compilers
we can get access to. Any comments?

Bob

void remove_duplicates(std::list<int>& in_list)
{
std::list<int>::iterator it = in_list.begin();
in_list.remove(*it);
}



嗯,为什么你认为它不会编译?

我认为它会编译,但它不会做它应该做的。
它的作用是删除in_list中的所有元素等于*(in_list.begin())的值。

最重要的是,如果in_list为空,则函数的行为很可能是未定义的。

问候,
Matthias



Well, why do YOU think it wouldn''t compile?

I think it would compile, but it wouldn''t do what it''s supposed to.
What it does is to remove all elements in in_list which equal the value
of *(in_list.begin()).

On top of that, if in_list is empty, the behavior of your function is
most probably undefined.

Regards,
Matthias




// test.cpp

#include< list>


void remove_duplicates(std :: list< int>& in_list)

{

std :: list< int> :: iterator it = in_list.begin();

in_list.remove(* it);

}



// test.cpp
#include <list>

void remove_duplicates(std::list<int>& in_list)
{
std::list<int>::iterator it = in_list.begin();
in_list.remove(*it);
}


g ++ -pedantic -Wall -std = c ++ 98 -o test test.cpp

- >没有错误。


这可能暗示这段代码至少在语法上是正确的:)


问候,

Matthias
g++ -pedantic -Wall -std=c++98 -o test test.cpp
--> no errors.

This may be a hint that this code is at least syntactically correct :)

Regards,
Matthias


这篇关于这是有效的代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆