C ++ using namespace std

Source: Internet
Author: User

From: http://blog.shuwo.org/archives/305/

Using namespace STD meaning:
Both using and namespace are keywords of C ++.
STD is the name of the namespace in which the standard library is located.
If the boost library is used, write using namespace boost;
If the C ++ standard library is used, write using namespace STD;

Expose the STD namespace. You can call the thing under the STD namespace. All classes and functions included in the namespace can be used.

General namespace usage:
This method is used as follows:

  • Namespace SDM
  • {
  • Int temp;
  • ......
  • }

  • There are three usage methods in a file:
    1> using namespace SDM;
    Then you can use the variable temp:
    Temp = 9;
    You can also use each other member in SDM.
    2> using SDM: temp;
    Only the temp variable in SDM can be used in this file.
    3> when using SDM: temp:
    Cout <SDM: temp <Endl;
    This is also called by other Members.

    Haha using namespace I think a kind of organization is involved in how to put a bunch of Code together and include them into a space. When using them, declare the space in which it is used

    For example, if both A and B have a name named Zhang San, you must first name the namespace of Class A if you want to use Zhang San of Class ), then all your commands on Michael can meet your expectation.

    I feel that language is the most troublesome thing to name. Due to human restrictions, it is difficult to use too complex names (variable names and function names), so it is easy to see name conflicts, so we have developed a namespace mechanism to ensure that you have enough names to use.
    With my experience, class also plays a similar role. functions with the same name of different classes can exist but do not conflict with each other, which is one of the manifestations.

    Namespace is a namespace. It is used to avoid name conflicts. You can use the same name in Different spaces without causing name conflicts.
    Its declaration is the same as declaring a class.
    Example:

  • Namespace {
  • Int F (){......};
  • Char g (){......};
  • ......}

  • There are several methods to use functions in a namespace:
    1-just like declaring a member function of a class, the class name is changed to the space name A: F ();
    2-you can use the using keyword using namespace A. In this way, the code will use the function in namespace.

    Extended read http://www.kuqin.com/language/20080107/3532.html
    # Include <iostream. h>
    This was something early in C ++.
    At that time, there was no template in this class library.
    # Incldue <iostream>
    Using namespace STD;
    The newly compiled STD is used.
    Is a different class from the original one.
    Yestemplate class
    6.0 compatibility
    In 2005, the early non-template library was abandoned.
    You can track cout in 6.0.
    Jump in and see, you can find that when you introduce different
    The databases that are jumped in are also different.

    ------------------ Gorgeous split line ----------------

    I:

    <Iostream> and <iostream. h> It is different. The former has no suffix. In fact, you can see in the include folder of your compiler that the two are two files. When you open the file, you will find that the code inside is different.

    Suffix. h header file C ++ standards have been explicitly proposed not to support, earlier implementation of the standard library function defined in the global space, declared in the. in the header file with the suffix H, the C ++ standard is used to distinguish it from C, and to correctly use the namespace, it is required that the header file does not use the suffix. h.

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

    II:

    Namespace refers to various visible ranges of identifiers.

    All identifiers in the C ++ standard library are defined in a namespace named STD.

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

    1. Specify the identifier directly. For example, 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 program can be written

    Cout <STD: Hex <3.4 <Endl;

    3. the most convenient method is to use using namespace STD;

    For example:

    # Include <iostream>
    # Include <sstream>
    # Include <string>
    Using namespace STD;
    In this way, all identifiers defined in the namespace STD are valid (exposed ). They are declared as global variables. The preceding statement can be written as follows:

    Cout

    Because the standard library is very large, the programmer may have the same name as a standard library when selecting the class name or function name. To avoid name conflicts caused by this situation, everything in the standard library is put in the namespace STD. However, this poses a new problem. Countless original C ++ Code relies on features in the pseudo-standard library that have been used for many years. They are all in the global space.

    Therefore, the header files such as <iostream. h> and <iostream> are designed to be compatible with the previous C ++ code, and to support new standards.

    The namespace STD encapsulates the name of the standard library. to distinguish the standard library from the previous header files, ". H" is generally not added"

     

    Because the standard library is very large, the programmer may have the same name as a standard library when selecting the class name or function name. Therefore, the third method may be conflicting.

    If you only use a few standard library functions, you should use the first or second method to avoid the same name as the function name in the standard library when defining your function, resulting in an error during compilation.

    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.