Differences between iostream and iostream. H & namespace usage

Source: Internet
Author: User

(1) differences between C ++ iostream and iostream. h

# Include <iostream. h> non-standard input/output stream
# Include <iostream> standard input/output stream

In C ++, "namespace definition" is introduced to avoid name definition conflicts. <Iostream. h>, the output can directly reference cout <X; Because <iostream. h> inherits the standard library files in C language and does not introduce the namespace definition. Therefore, you can use it directly.

When <iostream> is introduced in the Code, the output must reference STD: cout <X; if it is still based on the original method, it will be wrong. When <iostream> is used, STD: has the following methods:

1, using namespace STD;
Cout <X;
2, using STD: cout;
Cout <X;
3. The most basic STD: cout <X;

This time, you should know why using namespace STD; is used for # include <iostream>. If you don't need this, use the last two methods when using cout. The same applies to other header files. (Supplement: ". H" is a standard library function of non-standard C. If there is no ". H", it is necessary to use the command space, which is C ++ .)

(2) about namespace

Namespace: namespace. The reason for this is that there are too few words available for humans, and it is impossible for different people to write programs to have duplicate names in all variables, this problem is especially serious for libraries. If a variable or function with the same name appears in the library file written by two people (inevitably), the problem arises. To solve this problem, we have introduced the namespace concept by using namespace XXX. The library functions or variables you use are defined in the namespace, this will not cause unnecessary conflicts.

Instance:

# Include "stdafx. H"
# Include <iostream> // use the C ++ header file

Using namespace STD; // use the STD namespace
Namespace savitch1
{
Void greeting () // The greeting function defined in the savitaskspace
{
Cout <"hello from namespace savitch1. \ n ";
}
}
Namespace savitch2 // The greeting function defined in the savitch2 Space
{
Void greeting ()
{
Cout <"greeting from namespace savitch2. \ n ";
}
}
Void big_greeting ()
{
Cout <"a big global hello. \ n"; // directly use the STD Function
}
Int main (INT argc, char * argv [])
{
{
Using namespace savitch2;
Greeting (); // executes the greeting Function of space savitch2
}
{
Using namespace savit.pdf;
Greeting (); // executes the greeting function of the savitaskspace.
}
Big_greeting ();
Return 0;
}

The running result is:

Greeting from namespace savitch2.
Hello from namespace savitch1.
A big global hello.
Press any key to continue

As shown above, this avoids the use of functions with the same name.

 

Http://zhidao.baidu.com/question/115260239.html ()

Http://baike.baidu.com/view/159924.htm ()

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.