C++ cout cin格式控制符

void IOform(){
    cout << boolalpha << true << ' ' << false << endl << endl;

    cout << "Oct:" << oct << 20 << ' ' << 1024 << endl;
    cout << "Dec:" << dec << 20 << ' ' << 1024 << endl;
    cout << "Hex:" << hex << 20 << ' ' << 1024 << endl << endl;

    //输出前导字符串
    cout << showbase;
    cout << "Oct:" << oct << 255 << ' ' << 1024 << endl;
    cout << "Dec:" << dec << 255 << ' ' << 1024 << endl;
    cout << "Hex:" << hex << 255 << ' ' << 1024 << endl;
    cout << noshowbase << endl;

    //大写方式
    cout << uppercase;
    cout << showbase;
    cout << "Oct:" << oct << 255 << ' ' << 1024 << endl;
    cout << "Dec:" << dec << 255 << ' ' << 1024 << endl;
    cout << "Hex:" << hex << 255 << ' ' << 1024 << endl;
    cout << noshowbase;
    cout << nouppercase;
    cout << dec << endl;

    //浮点数
    //精度
    cout << "Percision:" << cout.precision() << endl;
    cout << sqrt(2.0) << endl << endl;
    //设置精度
    cout.precision(12);
    cout << "Percision:" << cout.precision() << endl;
    cout << sqrt(2.0) << endl << endl;

    cout << setprecision(24);// 需要头文件 #include <iomanip>
    cout << "Percision:" << cout.precision() << endl;
    cout << sqrt(2.0) << endl << endl;

    //打印小数点,小数点后和精度有关
    cout << 10.0 << endl;
    cout << showpoint << 10.0 << endl;
    cout << noshowpoint << endl;

    //补白
    cout << setw(10) << 1 << endl;  //指定下一项的最小空间,默认右对齐
    cout << 2 << endl << endl;

    cout << left;   //设置左对齐
    cout << setw(10) << 3 << 4 << endl << endl;

    cout << internal;   //控制负号左对齐,值右对齐
    cout << setw(10) << -16 << endl << endl;

    cout << setfill('#');   //设置填充符
    cout << setw(12) << setprecision(3) << 3.14 << endl << endl;
    cout << setfill(' ');

}

Output:

true false

Oct:24 2000
Dec:20 1024
Hex:14 400

Oct:0377 02000
Dec:255 1024
Hex:0xff 0x400

Oct:0377 02000
Dec:255 1024
Hex:0XFF 0X400

Percision:6
1.41421

Percision:12
1.41421356237

Percision:24
1.41421356237309514547462

10
10.0000000000000000000000

         1
2

3         4

-       16

########3.14


单字节读取

void IOunform(){
    int i;
    char ch;

    cin.get(ch);
    cout.put(ch);

    i = cin.get(); // 将下一个字节作为int返回
    cout << i;

    i = cin.peek(); // 将下一个字节作为INT返回,但不从流中删除它
    cout << i << ' ';
    cout << cin.get();

    int x;
    cin >> i;
    cin.unget();    //使得输入流向后移动,从而最后读取的值又回到流中。
    cin >> x;
    cout << i << x;

}

sstream 随机IO

void randIO(){
    istringstream is("0123456789");
    ostringstream os("0123456789");

    cout << "返回输入流的当前位置:" << is.tellg() << endl;
    cout << "返回输出流的当前位置:" << os.tellp() << endl;

    string tmp;
    is.seekg(6); //设置输入流位置
    cout << "返回输入流的当前位置:" << is.tellg() << endl;
    cout << "流中内容:" << is.str() << endl;
    is >> tmp;   // 从输入流中取值
    cout << tmp << endl;

    os.seekp(5, basic_ostringstream<ostringstream>::cur); //设置输出流位置 beg, cur, end
    cout << "返回输出流的当前位置:" << os.tellp() << endl;
    cout << "流中内容:" << os.str() << endl;
    os << "###";
    cout << "流中内容:" <<os.str() << endl;

}

Output:

返回输入流的当前位置:0
返回输出流的当前位置:0
返回输入流的当前位置:6
流中内容:0123456789
6789
返回输出流的当前位置:5
流中内容:0123456789
流中内容:01234###89

fstreamIO:

插眼

猜你喜欢

转载自blog.csdn.net/m_n_n/article/details/79860137
今日推荐