标准头文件和非标准头文件的差别

在以前的公司或者在现在的新公司,刚毕业的学生还有一些较有经验的编程员!都是使用着老的头文件导入方式,在以前的一个公司,王某,曹某,袁某这三个同事的代码中,把STL的用法简直是使用到"出神入化",王某使用的绝对路径,让人看不出他的C++的能力居然这么"厉害",对Unicode, UTF8......的编码,唔,也非常"精通",号称GDI+无人能比,“佩服,佩服”,一个恐怖的内存泄漏,让我痛苦了半个月,让人觉得可恨的是,王某一个自知都没有!还要对人的正确做法染三道四!

曹某还算可以,人也不错,可是就是不喜欢看书,COM自称非常厉害!是不是真的呢?

袁某,写程序不错,不过留下来的东东也"真不错",代码设计非常恶劣,一大堆的调试输出语句,滥用第三方库工具,呵,呵,只能一边“佩服”,STL也用得很乱!

还有一个赖某,呵,做事没有大志,有始没终,唉,不过,可喜的是这个小子学习特别快,改正自已的错误也特别的快,看到别人的错误,因为性格的问题,兼且人卑言轻,所以经常给这三个同事批评,不过,都过去了!赖某相信一定会闯出一片新天空的,无论在哪里,因为这个小子爱极了程序!

STL FAQ 上有对标准用法的一些建议,在《软件研发》杂志上也有这方面的讨论的文章,可惜,这么好的杂志也停刊了(可能上面的知识对中国的程序员太过超前了,别人说的,奇怪,我怎么看得明白了).

What's the difference between <xxx> and <xxx.h> headers

The headers in ISO Standard C++ don't have a .h suffix. This is something the standards committee changed from former practice. The details are different between headers that existed in C and those that are specific to C++.

The C++ standard library is guaranteed to have 18 standard headers from the C language. These headers come in two standard flavors, <cxxx> and <xxx.h> (where xxx is the basename of the header, such as stdio, stdlib, etc). These two flavors are identical except the <cxxx> versions provide their declarations in the std namespace only, and the <xxx.h> versions make them available both in std namespace and in the global namespace. The committee did it this way so that existing C code could continue to be compiled in C++. However the <xxx.h> versions are deprecated, meaning they are standard now but might not be part of the standard in future revisions. (See clause D.5 of the ISO C++ standard .)

The C++ standard library is also guaranteed to have 32 additional standard headers that have no direct counterparts in C, such as <iostream> , <string> , and <new> . You may see things like #include <iostream.h> and so on in old code, and some compiler vendors offer .h versions for that reason. But be careful: the .h versions, if available, may differ from the standard versions. And if you compile some units of a program with, for example, <iostream> and others with <iostream.h> , the program may not work.

For new projects, use only the <xxx> headers, not the <xxx.h> headers.

When modifying or extending existing code that uses the old header names, you should probably follow the practice in that code unless there's some important reason to switch to the standard headers (such as a facility available in standard <iostream> that was not available in the vendor's <iostream.h> ). If you need to standardize existing code, make sure to change all C++ headers in all program units including external libraries that get linked in to the final executable.

All of this affects the standard headers only. You're free to name your own headers anything you like; see [27.9].

标准库扩展了原库,例如新库<string>  还支持宽字符集的操作,所以我认为在现在大多数的编译器都已支持标准C++的情况下,所有的程序都应该使用标准头文件的导入方式!

对于名字空间 namespace std 的使用,C++ 的 FAQ 的回答是这样的

Should I use using namespace std in my code?

Probably not.

People don't like typing std:: over and over, and they discover that using namespace std lets the compiler see any std name, even if unqualified. The fly in that ointment is that it lets the compiler see any std name, even the ones you didn't think about. In other words, it can create name conflicts and ambiguities.

For example, suppose your code is counting things and you happen to use a variable or function named count. But the std library also uses the name count (it's one of the std algorithms), which could cause ambiguities.

Look, the whole point of namespaces is to prevent namespace collisions between two independently developed piles of code. The using-directive (that's the technical name for using namespace XYZ ) effectively dumps one namespace into another, which can subvert that goal. The using-directive exists for legacy C++ code and to ease the transition to namespaces, but you probably shouldn't use it on a regular basis, at least not in your new C++ code.

If you really want to avoid typing std:: , then you can either use something else called a using-declaration, or get over it and just type std:: (the un-solution):

  • Use a using-declaration, which brings in specific, selected names. For example, to allow your code to use the name cout without a std:: qualifier, you could insert using std::cout into your code. This is unlikely to cause confusion or ambiguity because the names you bring in are explicit.

     

    just type std:: (the un-solution):
  •  #include <vector>
     #include <iostream>

     
     void f(const std::vector<double>& v)
     {
       using std::cout;  
    //  a using-declaration that lets you use cout without qualification
     
       cout << "Values:";
       for (std::vector<double>::const_iterator p = v.begin(); p != v.end(); ++p)
         cout << ' ' << *p;
       cout << '/n';
     }

     

  • Get over it and
  •  #include <vector>
     #include <iostream>
     
     void f(const std::vector<double>& v)
     {
       std::cout << "Values:";
       for (std::vector<double>::const_iterator p = v.begin(); p != v.end(); ++p)
         std::cout << ' ' << *p;
       std::cout << '/n';
     }

     

    I personally find it's faster to type " std:: " than to decide, for each distinct std name, whether or not to include a using-declaration and if so, to find the best scope and add it there. But either way is fine. Just remember that you are part of a team, so make sure you use an approach that is consistent with the rest of your organization.

我个人也认为写多一个 std 没有什么不好,只是一个习惯的问题而已,写多了就是美,看多了就是爱嘛!!

况且减少很的错误,不好吗?

总之,遵从标准还是最好的选择!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值