(c + +) on using namespace Std

Source: Internet
Author: User

1, <iostream> and <iostream.h>

In your compiler include folder you can see that the two are two files, inside the code is not the same.

The header file with the suffix. h C + + standard has been explicitly not supported, the earlier implementation of the standard library functions defined in the global space, declared in the header file with the. h suffix;

The C + + standard differs from C in order to correctly use namespaces and specifies that the header file does not use a suffix. h.

So

When using <iostream.h>, the equivalent of calling library functions in C, using the global namespace, which is the early C + + implementation;

When using <iostream>, the header file does not have a global namespace defined, and namespace STD must be used so that cout is used correctly.

2. Using namespace Std
    • What is namespace?

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.

    • Use of namespace std:

Due to the concept of namespace, there are three options for using any identifier of the C + + standard library:

1. Specify identifier directly. For example Std::ostream rather than ostream. The complete statement is as follows:
Std::cout << Std::hex << 3.4 << Std::endl;

2. Use 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 in the namespace Std are valid (exposed). As if they were declared as global variables. Then the above statement can be written as follows:
cout << hex << 3.4 << Endl;
Because the standard library is very large, it is very likely that the programmer will have the same name or name of the class in the standard library as it is chosen. So in order to avoid the name conflict caused by this situation, everything in the standard library is placed in the namespace Std. But this poses a new problem. Countless original C + + code relies on functionality in pseudo-standard libraries that have been used for years, all in the global space.

So there are <iostream.h> and <iostream> and so on such a header file, one is to be compatible with the previous C + + code, one is to support the new standards.

Namespace STD Encapsulation is the name of the standard library, standard library in order to be different from the previous header file, generally do not add ". h"

    • When do I need to add using namespace std, when do not add?

What is the role of namespaces?

The function of a namespace is to prevent naming duplicates such as functions or variables.

For example, the STL has a string class, and if you also customize a string class, then the compiler will not be able to recognize which string the string is, and if you add a namespace, it will be well-separated. Assuming I put the custom string class in the AAA namespace, Aaa::string tells the compiler when it's used, that it calls the string class in the AAA namespace, and Std::string tells the compiler that The string class that is calling the Std namespace.

To answer the above questions, use the using namespace Std to tell the compiler that I'm using the contents of the Std namespace by default, so when naming duplicate definitions, such as functions and classes in the Std namespace, to avoid compiler confusion, do not add a using namespace STD, but if you need to invoke something inside, you can use a method that directly specifies the identifier, such as Std::cout.

  

Reference article:

Http://www.jb51.net/article/40018.htm

Http://wenwen.sogou.com/z/q227104716.htm

(c + +) on using namespace Std

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.