c++ - using namespace std 有什么用?什么时候用什么时候可以不用?
大家讲道理
大家讲道理 2017-04-17 12:03:11
0
5
1146

在 C++ 中 using namespace std; 有什么用,什么时候用什么时候可以不用,

#include<iostream.h>
using namespace std;
class point
{
public:
    int x;
    int y;
     void print()
    {
        cout<<"x="<<x<<endl;
        cout<<"y="<<y<<endl;
    }
};
void main()
{
    point pt;
    pt.x=0;
    pt.y=1;
    pt.print();
}

这段代码如果不注释掉 using namespace std; 运行就会提示出错,为什么?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(5)
黄舟

What’s included in “There’s………………” inside, the rest of it won’t be displayed on my top

黄舟

Theoretically, it can be unnecessary. But the premise is that you have to write the full name, such as std::cout.

大家讲道理

namespaceTo put it simply, it is like this, (take a made-up example)
Once upon a time, someone wrote a file operation module, which contained a open(char*) to open an ordinary file;
later He also wrote a database operation module, which also had a open(char*) used to open a database file;
one day he found that one of his programs used these two modules at the same time. As a result, the compilation failed because the functions had the same name~
The solution is very simple, just change the name~
But it is too troublesome to change so many open() in the two modules to file_open and database_open respectively, so the role of namespace It shows, he just needs to simply modify the two modules to namespace file{文件操作模块的原始内容}; and namespace database{数据库操作模块的原始内容};~ (just add a few lines of code)
so that when he needs to open the file, he writes file::open, and when he needs to open it, If you write database::open in the database, there will be no duplicate name problem
. But in this case, there will be no open function. All functions involving file operations must be prefixed with file::, and database operations require database::Prefix, he suddenly found that the code he wrote before could not be compiled~
Does the previous code need to be significantly changed? No, taking file operations as an example, as long as you add a sentence using namespace file;, the file namespace will be applied globally, and the compiler will try to find the file::open function, so the previous code can be compiled by just adding one more sentence~

Back to the point, because C++ libraries use common function names such as sort and swap, which can easily have the same name as user-defined functions. In order to avoid name conflicts, all library functions are placed by default. In the std namespace, if we also write a function called sort, there will be no naming conflict, because the full name of the thing in the library is std::sort

But we are usually very cautious when writing programs, and the naming is standardized enough. Duplication of names rarely occurs. On the contrary, the prefix of std:: is too redundant, and we have to use std::cout even to write output, so for the sake of convenience, here is One sentence using namespace std; to omit all prefixes. But this is equivalent to blocking the functions provided by namespace. At this time, if you write a swap function and the result is a naming conflict, don’t blame C++ for not providing a solution;

Of course, the previous description is inappropriate. Not only functions can be placed in namespace, variables, classes, structures, etc. are all allowed. cout is an example

巴扎黑

means to use the standard namespace. If not, you need to use std::cout to specify the namespace

刘奇

First of all, we need to understand the role of namespace; namespace is used to avoid naming conflicts;
For example, I defined a cout class myself for output;

//#mycout.h
class cout {
...
};

There is such a section in the program;


#include "mycout.h"
cout << "hello ";

There is no problem with compilation. The compiler finds my cout class and uses it for linking;

If I add the following statement, the system’s cout class is included and visible in the global scope:

#include <iostream>
using namespace std;

At this time, the compiler is confused. Now there are two cout classes. It is unclear which one to use, so it can only report an error;
In order to avoid system errors, we can decide which class to use; there are the following types Solution:

Remove using namespace std

This method will use my custom cout, and can also use other classes in the std space (you need to add the std:: prefix when using it, such as std::string)

#include "mycout.h"
#include <iostream>

cout << "hello";

Add using logo

If you want to use system cout, add std::

#include "mycout.h"
#include <iostream>

std::cout << "hello";

Add a namespace to my self-defined cout (recommended practice)


//#mycout.h
namespace CC{
    class cout {
    ...
    };
}
#include "mycout.h"
#include <iostream>
using namespace std;

cout << "hello"; //使用系统的cout
CC::cout << "colin"; //使用我自定义的cout类
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template