2.9 系统IO
iostream:
输入流
cin
; c 指代 character
输出流
cout
, cerr
(立即刷新缓冲区), clog
(缓冲区满后刷新)
命名空间
访问方式
namespace NameSpace1
{
void fun()
{
}
}
namespace NameSpace2
{
void fun()
{
}
}
int main()
{
// 不能写 fun();,会在全局命名空间中查找
NameSpace1::fun(); //最安全的写法
using namespace NameSpace2; //写在函数体内,不推荐写在函数体外,不要写在头文件内
fun();
}
名字空间与名称改编(name mangling)
- 查看 .o文件的链接
--nm ./main.cpp.o
, mangling不改变main函数名称
--nm ./main.cpp.o | c++filt -t
做demangling
C/C++ 系统IO比较
- printf
#include <cstdio>
int main()
{
int x=10;
flot y=10;
printf("Hello World")
printf("I hava %d apples\n", x);
printf("I hava %d apples\n", y);
}
-
cout:
--iostream
-- 不容易出错 -
C++20格式化库