应该采用哪种方式? <理论问题> [英] Which way should this be done? <Theory Question>

查看:65
本文介绍了应该采用哪种方式? <理论问题>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。


首先,我不确定使用的确切术语,所以我会问一个

的问题。然后是我的另一个问题。


当你使用来自iostream头文件的cout和cin之类的东西时,

这些叫什么? cout和cin功能?对于他们在头文件中列出的命令,适当的

术语是什么?或者它只是一个

"命令"?


我的下一个问题.....


我们在C ++课程中使用Deitel书。

书中的所有示例代码都显示他们使用以下格式来包含标题

文件并使用来自上方的术语。包含在其中。


#include< iostream>

使用std :: cout;

使用std :: cin;


我也曾在其他地方看到过


#include< iostream>

using namespace std ;


我一直在做的事情,以及我的老师所说的是来自

C的结转,是这个......


#include< iostream.h>


因此不需要任何使用命令。当你使用相当多的几个头文件时,那些使用语句会有点麻烦。


那么,什么是正确的这样做的方法?我相信每个人都有自己的方式,并且喜欢坚持自己的方式。通过各种方式获得的奖励或好处是什么?是否有任何或者是否b / b
严格的个人品味?


提前感谢任何想法。


DV

Hello.

First off, I am not sure of the exact jargon used, so I will ask a
question regarding it. Then on to my other question.

When you use things like cout and cin from the iostream header file,
what are those called? Is cout and cin functions? What is the proper
term for them a command listed in a header file? Or is it just a
"command"?

My next question.....

We use a Deitel book in my C++ class. All of their sample code in the
book shows that they use the following format for including header
files and using the "term from above" contained in them.

#include <iostream>
using std::cout;
using std::cin;

I have also seen it in other places as

#include <iostream>
using namespace std;

What I have always done, and what my teacher says is a carry over from
C, is this....

#include <iostream.h>

so that none of the using commands are needed. When your using quite a
few header files, those using statements get a bit cumbersome.

So, what is the "proper" way to do it? I am sure everyone has their
own way of doing it and likes to stick to their way. What are the
rewards or benifits from doing it each way? Are there any or is it
strictly personal taste?

Thanks in advance for any ideas.

DV

推荐答案

" da Vinci" < BL *** @ blank.com>在消息中写道

news:pv ******************************** @ 4ax.com ......

|当您使用来自iostream头文件的cout和cin之类的内容时,

|这叫什么? cout和cin功能?什么是正确的

|对于他们来说,在头文件中列出了一个命令?或者只是一个

| "命令"


cin和cout是来自iostream类的预定义对象。还有

clog和cerr(几乎大致相同)写入

错误流(通常只是某些系统上的屏幕,除非被覆盖为

你可以用Linux做什么。)


你是什么意思命令?你指的是宏,函数/类

声明,编译指示,......?我只能假设你指的是

使用:这是一个声明。


|我们在C ++课程中使用Deitel书。他们所有的示例代码都在

|中本书显示他们使用以下格式包含标题

|文件并使用来自上方的术语包含在其中。

|

| #include< iostream>

|使用std :: cout;

|使用std :: cin;


std是指C ++标准命名空间。一切都在这个命名空间中。一个

名称空间基本上是名称与其他类似名称重合的方式

(例如你可以写)。如果你要写一个类向量<>对于

的例子,你可以区分实例化你的和

标准:


vector< int> myvec会; //你的矢量类型

std :: vector< int> stdVec; //标准载体


|我也在其他地方看过它

|

| #include< iostream>

|使用命名空间std;


这将每个名称,函数,类,类型(等)带入全局

命名空间。这意味着您可能会与标准

中的某些内容发生冲突(例如带有签名的缩进函数名称)。您的教授最终应该在您的课程中涵盖

命名空间。


|我一直在做的事情,以及我的老师所说的是来自

| C,这是....

|

| #include< iostream.h>


你需要一位更好的老师。 iostream.h不是来自C,而是来自C ++

之前有一个标准。这是古老的,只保留了

向后兼容性(对于懒惰的驴,不会更新他们的

软件以反映ANSI / ISO IEC 14882-1998)。所有标准标题都没有

扩展名(没有.h)。对于标准的C标题,同样如此,但'c''是名字前面的
(stdio.h变为cstdio)。


|这样就不需要任何使用命令了。当你使用相当的

|几个头文件,那些使用语句有点麻烦。


只需编写using namespace std就没有错。永远不要使用

iostream.h。


|那么,什么是适当的这样做的方法?我相信每个人都有他们的

|这样做的方式,并喜欢坚持自己的方式。什么是

|从各方面做的奖励还是好处?有没有或是它

|严格的个人品味?


正确的方法是在std命名空间中确定(作为你的老师)资格

。没有涉及个人品味,只需额外输入

。我能做到:


std :: vector< int> vecInt;


使用std :: vector;

vector< int> vecInt;


使用命名空间std;

vector< int> vecInt;


所有都是一样的。唯一的区别是使用命名空间std带来

一切都在范围内;
"da Vinci" <bl***@blank.com> wrote in message
news:pv********************************@4ax.com...
| When you use things like cout and cin from the iostream header file,
| what are those called? Is cout and cin functions? What is the proper
| term for them a command listed in a header file? Or is it just a
| "command"?

cin and cout are predifined objects from the iostream classes. There is also
clog and cerr (which are pretty much the same mostly) that write to the
error stream (usually just the screen on some systems, unless overridden as
you can do in Linux).

What do you mean by "command"? Are you referring to macros, function/class
declarations, pragmas,...? I can only assume that you are referring to
"using": it''s a statement.

| We use a Deitel book in my C++ class. All of their sample code in the
| book shows that they use the following format for including header
| files and using the "term from above" contained in them.
|
| #include <iostream>
| using std::cout;
| using std::cin;

std refers to the C++ standard namespace. Everything is in this namespace. A
namespace is basically a way for names to coincide with other similar names
(that you may write for example). If you were to write a class vector<> for
example, you could distinguish between instantiating yours and the
standards:

vector<int> myVec; // Your vector type
std::vector<int> stdVec; // standard vector

| I have also seen it in other places as
|
| #include <iostream>
| using namespace std;

This brings every name, function, class, type (etc) into the global
namespace. This means that you may conflict with something from the standard
(like indentical function names w/ signatures). Your professor should cover
namespaces in your course eventually.

| What I have always done, and what my teacher says is a carry over from
| C, is this....
|
| #include <iostream.h>

You need a better teacher. iostream.h is not from C, but rather from C++
before there was a standard for it. It is archaic and only kept for
backwards compatibility (for that lazy asses that won''t update their
software to reflect ANSI/ISO IEC 14882-1998). All standard headers carry no
extension (no .h). For the standard C headers, the same is true but a ''c'' is
prepended to their name (stdio.h becomes cstdio).

| so that none of the using commands are needed. When your using quite a
| few header files, those using statements get a bit cumbersome.

There is nothing wrong with just writing "using namespace std". Never use
iostream.h.

| So, what is the "proper" way to do it? I am sure everyone has their
| own way of doing it and likes to stick to their way. What are the
| rewards or benifits from doing it each way? Are there any or is it
| strictly personal taste?

The proper way to do it is to qualify (as your teacher about this) anything
in the std namespace. There is no personal taste involved, just the extra
typing. I could do:

std::vector<int> vecInt;

using std::vector;
vector<int> vecInt;

using namespace std;
vector<int> vecInt;

All are the same. The only difference is the using namespace std brings
everything into scope;




" Greg P." ; < no@spam.sam>在消息中写道

新闻:9%***************** @ newsread4.news.pas.earthl ink.net ...

"Greg P." <no@spam.sam> wrote in message
news:9%*****************@newsread4.news.pas.earthl ink.net...
达芬奇 < BL *** @ blank.com>在消息中写道

|那么,什么是适当的这样做的方法?我相信每个人都有他们的
|这样做的方式,并喜欢坚持自己的方式。什么是
|从各方面做的奖励还是好处?有没有或是
|严格的个人品味?

正确的方法是在std名称空间中获得资格(作为你的老师)。没有个人品味,只需额外打字。我能做到:

std :: vector< int> vecInt;

使用std :: vector;
vector< int> vecInt;

使用命名空间std;
vector< int> vecInt;

一切都是一样的。唯一的区别是使用命名空间std将所有内容带入范围;
"da Vinci" <bl***@blank.com> wrote in message

| So, what is the "proper" way to do it? I am sure everyone has their
| own way of doing it and likes to stick to their way. What are the
| rewards or benifits from doing it each way? Are there any or is it
| strictly personal taste?

The proper way to do it is to qualify (as your teacher about this)
anything in the std namespace. There is no personal taste involved,
just the extra typing. I could do:

std::vector<int> vecInt;

using std::vector;
vector<int> vecInt;

using namespace std;
vector<int> vecInt;

All are the same. The only difference is the using namespace std brings
everything into scope;




他们并非完全相同。正如Stroustrup在C ++编程中指出的那样,使用声明将一个名称添加到局部范围,而使用指令则没有。大部分时间它可能无关紧要,但它可以做到。




They''re not all the same. As Stroustrup points out in The C++ Programming
Language, C.10, a using declaration adds a name to a local scope whereas a
using directive doesn''t. Much of the time it may not matter but it can do.



感谢您的知识到目前为止!

但是现在我还有别的事情让我不安。每个人都有

几乎指出在包含

语句中使用.h表格不是标准的c ++,不应该使用。


使用namespace std;有什么好处?线?使用使用std :: cout;可以获得什么?b $ b优势?我的意思是

优于使用include语句下的.h ....


在我看来,添加所有这些代码行对于名称空间

包含这样是浪费时间,你可以在

包含行中使用.h。


我知道ANSI不会改变这样的东西,在代码中使代码变得更难,或者更加笨重。那么,他们的原因是什么?

这是否使程序在许多不同的平台上兼容?

编译器?它是否节省了程序的内存使用量?


再次,我只是一个初学者。我想知道整个故事而不是

只是这样做,因为ANSI这样说!回答。我喜欢

深入,感觉,知识背后。


到目前为止我感谢所有的帮助。


DV
Thanks for the knowledge so far!

But now I have something else troubling me about this. Everyone has
pretty much pointed out that using the .h form in the include
statement is not standard c++ and should not be used.

What advantage do you get over using the "namespace std;" line? What
advantage would you get using the "using std::cout;". I mean the
advantage over using just the .h under the include statement....

It seems to me that adding all of these lines of code for namespace
inclusions and such is a waste of time when you can just use the .h in
the include line.

I know that ANSI would not change something like this, make it harder
or more bulky in the code, for no reason. So, what was their reason?
Does this make programs compatible across many different platforms or
compilers? Does it save on the memory useage of the program?

Again, I am just a beginner. I like to know the whole story instead of
just "Do it this way because ANSI says so!" answer. I like having the
indepth, behind the sense, kind of knowledge.

I appreciate all the help so far.

DV


这篇关于应该采用哪种方式? &lt;理论问题&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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