【C++笔记】C++的头文件和std命名空间

一、在C++引入命名空间之前,并没有自己的命名空间。都是沿用以前C语言的习惯,直接是.h文件作为库文件。引入命名空间后,C++进行规范化管理,将以前C语言的头文件库,有命名空间以后的C++头文件库都复制增加一份后归入std命名空间。其中:原来C语言的头文件库如stdio.h在std命名空间中变为cstdio(没有h,前面加了c),原来C++中的头文件去掉h,如iostream.h在std命名空间中变为iostream。原来C++中的库依然可用,但不再维护,也不推荐实用,推荐的都是std命名空间中的库。

标准用法实例:

#include <cstdio>
int main(){
    std::printf("http://c.biancheng.net\n");
    return 0;
}

忠告:写C++程序就尽量用新版的C++的语法规范,调用的库也是C++新规范下的库。写C语言可以用原来C语言的库。

 

1.如果希望在所有函数中都使用命名空间 std,可以将它声明在全局范围中,

#include <iostream>

//声明命名空间std
using namespace std;

void func(){
    cout<<"http://c.biancheng.net"<<endl;
}

int main(){
    cout<<"C语言中文网"<<endl;
    func();

    return 0;
}

2.如果是局部使用的话,则在函数内部声明即可。

#include <iostream>

void func(){
    //必须重新声明
    using namespace std;
    cout<<"http://c.biancheng.net"<<endl;
}

int main(){
    //声明命名空间std
    using namespace std;
   
    cout<<"C语言中文网"<<endl;
    func();

    return 0;
}

二、输入输出

C语言是stdio.h,里面是printf和scanf.C++ 是iostream,里面是cin 和cout.

用法示例:

#include<iostream>
using namespace std;
int main(){
    int x;
    float y;
    cout<<"Please input an int number and a float number:"<<endl;
    cin>>x>>y;
    cout<<"The int number is x= "<<x<<endl;
    cout<<"The float number is y= "<<y<<endl;   
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值