zoukankan      html  css  js  c++  java
  • c++输出左右对齐设置

    #include<iostream>
    int main(){
        using std::cout;
        cout.setf(std::ios::left);
        int w = cout.width();
        cout << "default field width = " << w << "
    ";
        cout.width(5);
        cout << "N" << ":";
        cout.width(8);
        cout << "N * N" << "
    ";
       for (long i = 1; i <= 100; i *= 10){
            cout.width(5);
            cout << i << ':';
            cout.width(8);
            cout << i * i << "
    ";
        }
        return 0;
    }

    注意那行绿色的

    是对全局有效,左对齐.

    (右对齐是默认的)

    不过还有一个

    1    cout<<right<<setw(4)<<"111";
    2    cout<<left<<setw(5)<<"222";
    这个就可以实现左边右边
    1    cout<<right<<setw(4)<<setfill('*')<<"111";
    2    cout<<left<<setw(5)<<"222";
    setfill慎用!
    他也是对全局有效的!
  • 相关阅读:
    【模板】并查集
    排队布局
    list
    js获取宽度
    js获取按键
    sublime 自定义快捷代码
    file
    git add Untracked files
    git branch
    git
  • 原文地址:https://www.cnblogs.com/zhmlzhml/p/12457717.html
Copyright © 2011-2022 走看看