应该包含哪些标头才能在 Linux 上的 c++ 代码中保留前缀 std::
What header(s) should be included to leave the prefix std:: in my c++ code on Linux?
我想在Linux操作系统上的c ++代码中使用没有std::的语句(例如cout而不是std::cout,map<>而不是std::map<>等)。执行它需要哪些标头?
正如评论者指出的那样:没有包含来做到这一点,你只需要像往常一样包含你的文件,然后写一个using namespace
语句
#include <iostream> // cout
#include <map> // map
using namespace std; // bring this entire namespace into scope
但是;你应该注意,评论者已经指出,这是一个糟糕的想法,尤其是在头文件中。对于您需要编写的额外几个字符,您可以节省数小时的麻烦。如果你真的反对写std::
,请考虑限制你这样做的范围
{
// lots of console printing:
using std::cout;
cout << "";
...
}
// Now you'd need to write std::cout again
这是独立于操作系统的(Linux的解决方案与其他操作系统相同)
如果您有要引入默认命名空间的类,您可以使用 using
来为它们设置别名。 例如:
using std::cout;
您可以使用 using namespace
导入整个命名空间,但您需要首先阅读并理解"为什么using namespace std
被认为是不好的做法?
这不是
#including 某个标题的问题,可以使用 using 命名空间语句省略显式命名空间。
如果你想省略(真的:将所有 std:: 命名空间项放入全局命名空间,这可能不是真正的好做法),你可以使用:
using namespace std;
通常在 #includes 之后,但在引用 std 内容的任何代码之前。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 此代码是否违反一个定义规则
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 为什么在这个代码结束循环中没有得到结束
- 在c代码之间共享数据的最佳方式
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值