C + + using namespace detailed

Source: Internet
Author: User
Tags ranges

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.

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.

Suffix is. H's header file C + + standard has been explicitly proposed does not support, the earlier implementation of the standard library functions defined in the global space, declared in the band. H suffix in the header file, C + + standard for and C distinguish between, also in order to correctly use the namespace, the provision of the header file does not use the suffix. H.

Therefore, when using <iostream.h>, it is equivalent to calling the library function in C, using the global namespace, the early C + + implementation, and when using < Iostream>, the header file does not define a global namespace. Namespace STD must be used so that cout can be used correctly.

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 instead of 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.

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.