C + + using namespace std usage in-depth parsing of _c languages

Source: Internet
Author: User

One:
<iostream> and <iostream.h> are not the same, the former has no suffix, in fact, in your compiler include folder you can see that the two are two files, open the file will find that the code inside is not the same.

The C + + standard for the header file suffix. h has been explicitly raised and not supported, the earlier implementation defined the standard library functionality in the global space, declared in the header file with the. h suffix, the C + + standard to distinguish it from C, and in order to use the namespace correctly, it is stipulated that the header file does not use a suffix. h.

Therefore, when using <iostream.h>, the equivalent of calling the library function in C, using the global namespace, that is, the early C + + implementation;

When using <iostream>, the header file does not define a global namespace and must use the namespace Std to properly use cout.

Two:
The so-called namespace refers to the various visible ranges of identifiers.

All identifiers in the C + + standard library are defined in a namespace called Std.

Because of the concept of namespace, you can have three choices when using any identifier of the C + + standard library:

1, directly specify the identifier. such as Std::ostream rather than ostream. The complete statement is as follows:
Std::cout << Std::hex << 3.4 << Std::endl;

2. Use the Using keyword.
Using Std::cout;
Using Std::endl;

The above procedure can be written
cout << Std::hex << 3.4 << Endl;

3, the most convenient is to use using namespace std;

For example:
#include <iostream>
#include <sstream>
#include <string>
using namespace Std;

All identifiers defined within the namespace Std are valid (exposed). As if they were declared as global variables. So the above statements can be written as follows:
cout << hex << 3.4 << Endl;
Because the standard library is very large, the programmer in the selection of the name of the class or function is likely to be the same name in the standard library. So in order to avoid the name conflict caused by this situation, everything in the standard library is placed in the STD of the name space. But this poses a new problem. Countless original C + + code relies on the functionality in the pseudo standard library that has been in use for many years, and they are all under global space.

So there are headers such as <iostream.h> and <iostream>, one for compatibility with previous C + + code, and one for supporting new standards.

The namespace std encapsulates the name of the standard library, which distinguishes it from the previous header file and generally does not add ". h" to the code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.