C++标准输出流

73 阅读1分钟

标准输出流对象 cout

cout.flush()

cout.put()

cout.write()

cout.width()

cout.fill()

cout.setf(标记)

标准输出流常见api编程案例 

#include <iostream>
#include <cstring>
#include <iomanip>

using namespace std;

int main01(void)
{

    cout<<"hello world"<<endl;
    cout.put('h').put('e').put('l').put('l').put('e').put('\n');

    cout.write("hello world",4);

    printf("\n");
    char buf[] = "hello world";
    cout.write(buf,strlen(buf));

    printf("\n");
    cout.write(buf,strlen(buf)-6);
    printf("\n");

    cout.write(buf,strlen(buf)+6);
    printf("\n");

    return 0;
}

int main(void)
{

    //使用类成员函数
    cout<<"---let's go---"<<endl;
    cout.width(30); //设置下一次输出的宽度为30,默认右对齐
    cout.fill('*'); //因为指定了宽度,所以可以用fill来填充空白位置

    cout.setf(ios::showbase);   //输出时显示基数 0 0x
    cout.setf(ios::internal);   //将填充字符放到符号和数字之间

    cout<<hex<<123<<endl;
    cout<<"---end---"<<endl;

    //使用控制阈
    cout<<"<<<<< start >>>>>"<<endl;

    cout<<setw(30)
        <<setfill('*')
        <<setiosflags(ios::showbase)
        <<setiosflags(ios::internal)
        <<hex
        <<123
        <<endl;

    cout<<"<<<<<  end  >>>>>"<<endl;


    return 0;
}