如何使用cin和cout

1、使用前加前缀“std::”。

#include<iostream>
int main()
{
    int i;
    std::cin>>i;
    std::cout<<"hello world"<<std::endl; 
    return 0;
}

2、使用using指令直接获取std命名空间中元素的访问权。

#include<iostream>
using namespace std;
int main()
{
    int i;
    cin>>i;
    cout<<"hello world"<<endl; 
    return 0;
}

3、使用using指令声明标识符cout、cin、endl。

#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
    int i;
    cin>>i;
    cout<<"hello world"<<endl; 
    return 0;
}

4、使用宏来替代。和第一个本质相同。

#include<iostream>
#define cin std::cin
#define cout std::cout
#define endl std::endl
int main()
{
    int i;
    cin>>i;
    cout<<"hello world"<<endl;
    return 0;
}

5、使用<iostream.h>头文件。对,你用< iostream >需要加std而使用这个C语言风格的头文件就能直接用cin和cout。
注意:有些编译器内可能不包括这个头文件,所以无法使用。较老的编译器和兼容性好的编译器能支持。

#include<iostream.h>
int main()
{
    int i;
    cin>>i;
    cout<<"hello world"<<endl; 
    return 0;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值