zoukankan      html  css  js  c++  java
  • cout的输出格式初探2

     1 #include <iostream>
     2 #include <iomanip>
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     cout<<"Chapter 1"<<endl;
     8     cout<<" ";
     9     cout.setf(ios::left); //设置对齐方式为left
    10     cout.width(7); //设置宽度为7,不足用空格填充
    11     cout<<"1.1";
    12     cout<<"What's C language";
    13     cout.unsetf(ios::left); //取消对齐方式,用缺省right方式
    14     cout.fill('.'); //设置填充方式
    15     cout.width(30); //设置宽度,只对下条输出有用
    16     cout<<1<<endl;
    17     cout<<" ";
    18     cout.width(7); //设置宽度
    19     cout.setf(ios::left); //设置对齐方式为left
    20     cout.fill(' '); //设置填充,缺省为空格
    21     cout<<"1.11";
    22     cout<<"The history of C";
    23     cout.unsetf(ios::left); //取消对齐方式
    24     cout.fill('.');
    25     cout.width(30);
    26     cout<<58<<endl;
    27     cout.fill(' ');
    28     cout<<"Chapter 2"<<endl;
    29     
    30     cout<<"----------------------"<<endl;
    31     
    32     cout<<"Chapter 1"<<endl;
    33     cout<<" ";
    34     cout<<setiosflags(ios::left)<<setw(7); //设置宽度为7,left对齐方式
    35     cout<<"1.1";
    36     cout<<"What's C language";
    37     cout<<resetiosflags(ios::left); //取消对齐方式
    38     cout<<setfill('.')<<setw(30)<<1<<endl; //宽度为30,填充为'.'输出
    39     cout<<setfill(' '); //恢复填充为空格
    40     cout<<" ";
    41     cout<<setw(7)<<setiosflags(ios::left); //设置宽度为7,left对齐方式
    42     cout<<"1.11";
    43     cout<<"The history of C";
    44     cout<<resetiosflags(ios::left); //取消对齐方式
    45     cout<<setfill('.')<<setw(30)<<58<<endl; //宽度为30,填充为'.'输出
    46     cout<<setfill(' ')<<"Chapter 2"<<endl;
    47     
    48     return 0;
    49 }

    程序运行结果:

  • 相关阅读:
    Mysql 权限命令整理大全
    阿里云ECS发送邮件失败
    彻底删除Kafka中的topic
    mysql Slave 启动失败
    mysql双主热备
    mysql 主从笔记
    mysql主从同步的键值冲突问题的解决方法
    python0.2----如何在windows下搭建最简洁的python环境
    内存0.1---内存里数据的表示形式以及进制转换
    python0.1-----pyhon的优缺点,为何学习python
  • 原文地址:https://www.cnblogs.com/GarfieldEr007/p/4632200.html
Copyright © 2011-2022 走看看