Use of C ++ using namespace std Detailed and namespace

Source: https: //blog.csdn.net/Bruce_0712/article/details/72824668

The so-called namespace, refers to various visible range identifier. All identifiers C ++ standard library are defined in the namespace std called in.
A:
<iostream> and <iostream.h> is not the same, the former has no suffix, in fact, in your compiler include folder inside you can see, the two are two files, open the file you will find, inside the code is not the same.

Suffix .h header files c ++ standard has been clearly do not support, and earlier implementation of the standard library functions defined in the global space, declared in the header files with a .h suffix, the standard c ++ and C in order to distinguish, also In order to properly use namespaces, do not use the provisions of the header file suffix .h.

Thus, when using <iostream.h>, equivalent to calling the library functions (c), using the global namespace, i.e. early c ++ implementation; When <iostream> when header file does not define the global namespace you must use the namespace std; in order to properly use cout.

II:
The so-called namespace, refers to various visible range identifier.
All identifiers C ++ standard library are defined in the namespace std called in.
Since the concept of namespace, when using the C ++ standard library of any identifier, you can have three choices:

1, directly specified identifier. For example, rather than std :: ostream ostream. Full statement is as follows:

std :: cout << std :: hex << std :: endl << 3.4;

2, using keywords.

std :: cout a using;
a using std :: endl;

above procedures can be written as

<< << std :: hex COUT 3.4 << endl;

. 3, is the most convenient to use using namespace std;
example:
#include <the iostream>
#include <sstream>
#include <String>
using namespace std;
this namespace std within the definition of all identifiers are valid (exposure). If they are 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, the programmers in the name of the class or function name selected it is very likely a name and standard library the same. So in order to avoid name conflicts caused by this situation, put everything in the standard library are placed in namespace std. But it will bring a new problem. Numerous original C ++ code rely on the use of the function of many years of pseudo-standard library, they are in a global space.  

         So there <iostream.h> and <iostream> header file, and so this one is for compatibility with previous C ++ code to support a new standard.
Namespace std package is the name of the standard library, the standard library in order and header files difference before, usually without the ".h"

              C ++ namespace to use

C ++ language provides a global namespace namespace, avoid situations that cause global naming conflict. As an example, consider the following two headers:
// One.


Somelib.h //
class String {...};
if defined in the above manner, the file can not contain two heads in the same program as the String class conflict.
The so-called namespace, the library name is a way to package up, it's like a road fence erected at each library. such as:

namespace myown1{

string user_name = "myown1 "; 

namespace myown2{

string user_name = "myown2 "; 
}

main int () 

COUT << "/ n-" << "the Hello," <<myown1 // :: user_name user_name variable qualifier myown1 access with namespace 
<< "... and goodbye / n !";

cout << "/ n" < < "Hello," <<myown2 :: user_name // user_name variable qualifier myown2 access with namespace 
<< "... and goodbye / n !";

return 0; 
}

 

Unnamed namespace

standard C ++ introduced namespaces, in addition to name members to avoid conflicts, you can also keep your code locally, thereby protecting the code is not used illegally. If your main purpose of the latter, and also serve as an alternate namespace take a good, meaningful, and with others namespace does not worry about the name of the same name, then the standard C ++ also allows you to define an unnamed namespace. You can (outside unnamed namespace), direct use of unnamed namespace member names in the current compilation unit, but outside the current compilation unit, it is not visible.

Unknown namespace defined format:

namespace {

statement Alternatively sequence

}

In fact, as defined above, there is an implicit use of the equivalent instruction) :( in the C ++ standard

namespace $$$ {

statement sequence optionally

}

the using namespace $ $$;

example:

namespace {

int I;

void F () {/ * ...... * /}

}

int main () {

I = 0; // can be directly used in the unnamed namespace members I

F (); // can be used directly unnamed namespace members of f ()


Now even if the String class in the same program will not conflict because they were turned into: one :: String () and Somelib :: String ()
This allows you to namespace by declaring to distinguish between different classes or function wait.

 

Such as C ++ Standard Library defines the namespace: std, wherein the container comprises a vector, the following example:
#include "the stdafx.h"
#include <Vector>
#include <the iostream>
#include <algorithm>
the using namespace STD;
int main (int argc , char * the argv [])
{
const int = ArraySize. 7;
int IA [ArraySize] = {0,1,2,3,4,5};
File: // custom container Vector
Vector <int> ivect (IA, IA ArraySize +);
Vector <int> :: Find Iterator IT1 = (ivect.begin (), ivect.end (),. 4);
IF (IT1 ivect.end == ())
  COUT << "Not found. 4" < <endl;
the else
  COUT << "found. 4" << endl << * IT1;
return 0;
}
The output is: 4 found 4.

发布了7 篇原创文章 · 获赞 13 · 访问量 2万+

Guess you like

Origin blog.csdn.net/xingsongyu/article/details/103629747