zoukankan      html  css  js  c++  java
  • 宽度,对齐方式的设置

    一、输出宽度

    1.使用width函数控制

     1 #include<iostream>
     2 using namespace std;
     3 
     4 int main() {
     5     double values[] = { 1.23,35.36,653.7,4358.24 };
     6     for (int i = 0; i < 4; i++) {
     7         cout.width(10);
     8         cout << values[i] << endl;
     9     }
    10     return 0;
    11 }

      默认为右对齐。运行结果如下:

    2.使用set操作符控制,有头文件#include<iomanip>

     1 #include<iostream>
     2 #include<iomanip>
     3 #include<string>
     4 using namespace std;
     5 
     6 int main() {
     7     double values[] = { 1.23,35.36,653.7,4358.24 };
     8     string names[] = { "Zoot","Jimmy","Al","Stan" };
     9     for (int i = 0; i < 4; i++) {
    10         cout << setw(6) << names[i] << setw(10) << values[i] << endl;
    11     }
    12     return 0;
    13 }

    运行结果如下:

    二、输出宽度

     通过使用带参的setiosflags操作符来设置左对齐,ios_base::left是ios_base的静态常量,必须使用ios_base::前缀。用resetiosflags(ios_base::left)来关闭左对齐标志。

     1 #include<iostream>
     2 #include<iomanip>
     3 #include<string>
     4 using namespace std;
     5 
     6 int main() {
     7     double values[] = { 1.23,35.36,653.7,4358.24 };
     8     string names[] = { "Zoot","Jimmy","Al","Stan" };
     9     for (int i = 0; i < 4; i++) {
    10         cout << setiosflags(ios_base::left) << setw(6) << names[i] << resetiosflags(ios_base::left) << setw(10) << values[i] << endl;
    11     }
    12     return 0;
    13 }

    运行结果如下:

  • 相关阅读:
    Shodan新手入坑指南
    linux 下查看网卡工作速率
    centos关闭ipv6
    springBoot----@ConditionalOnxxx相关注解总结
    ElasticSearch Root身份运行
    CentOS6 Install kafka
    CentOS 7 中firewall-cmd命令
    sensu
    metrics+spring+influxdb
    SpringBoot(十二):SpringBoot整合Kafka
  • 原文地址:https://www.cnblogs.com/gzu_zb/p/9373954.html
Copyright © 2011-2022 走看看