标准输入流

#include<iostream>
#include<iomanip>
using namespace std;
#if 1
int main()
{
    int a=21;
    //cout.setf设置格式
    cout.setf(ios::showbase);//为整数加一个进制前缀
    cout<<"dec:"<<a<<endl;//十进制
    cout.unsetf(ios:dec);//取消十进制格式设置
    cout.setf(ios::hex);
    cout<<"hex:"<<a<<endl;//十六进制
    cout.unsetf(ios::hex);
    cout.setf(ios::oct);
    cout<<"oct:"<<a<<endl;//8进制
    cout.unsetf(ios::oct);
    const char *pt="jack";
//直接利用格式成员函数
    cout.width(10);//指定域宽
    cout<<pt<<endl;
    cout.width(10);//一次性输出 每次都要设置
    cout.fill('*');//用*填充
    cout<<pt<<endl;   
    double pi=22.0/7.0;
    cout<<pi<<endl; 
    cout.setf(ios::scientific);
    cout<<pi<<endl; 
    cout.unsetf(ios::scientific);
    //第三种 用流方式输出 四个字节宽度 右对齐 用0填充
    cout<<setw(4)<<right<<setfill('0')<<1<<endl;
    //显示前缀八进制
    cout<<showbase<<oct<<21<<endl;

    bool b=false;
    cout<<b<<endl;
    cout<<boolalpha<<b<<endl;
    cout<<noboolalpha<<b<<endl;
    return 0;
}
#endif  //流格式化 记住五个常用
#if 0
//cout 标准输出对象(有缓冲区) cerr标准错误流(无缓冲区) clog标准错误流(有缓冲区)
//cout 输出可以重定向到一个文件中 cerr没缓冲区必须显示在显示器上
int main()
{
    cout<<"cout"<<endl;
    cerr<<"cerr"<<endl;
    cerr<<"hello,world"<<endl;
return 0;
}
#endif//重定向
#if 0
{
    cout<<"a";//没有清空缓冲区
    cout<<"b"<<ends;//等于插了个\0字符
    cout<<"c"<<endl;//等于插了个\n字符
    cout<<"d"<<flush;//刷新缓冲区
    //获取单个字符
    char c=cin.get();
    cout<<c<<endl;
    cout.put(c);//输出一个字符
    //const char* c="abc";//不行 必须为单个字符
    //cout.put(c);
    cout.put(c)<<endl;//换行
    endl(cout<<"HELLO");//函数方式换行
    //获取行
    char str[200]={0,};//输入123 abc 只会读到123 后面的abc会在缓冲区里面
    cin>>str;
    cout<<str;    
    cin.getline(str,20);//(放哪里,获取大小)
    cout<<str<<endl;
    cin.getline(str,20,'/');没有/读满20个有/的话就到此为止
    cout<<str<<endl;
return 0;
}
#endif//标准输入输出流

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值