Right Justifying output stream in C++

前端 未结 3 524
遥遥无期
遥遥无期 2020-11-30 14:00

I\'m working in C++. I\'m given a 10 digit string (char array) that may or may not have 3 dashes in it (making it up to 13 characters). Is there a built in way with the st

相关标签:
3条回答
  • 2020-11-30 14:09

    You need to use std::setw in conjunction with std::right.

    #include <iostream>
    #include <iomanip>
    
    int main(void)
    {
       std::cout << std::right << std::setw(13) << "foobar" << std::endl;
       return 0;
    }
    
    0 讨论(0)
  • 2020-11-30 14:09

    See "setw" and "right" in your favorite C++ (iostream) reference for further details:

     cout << setw(13) << right << your_string;
    
    0 讨论(0)
  • 2020-11-30 14:27

    Yes. You can use setw() to set the width. The default justification is right-justified, and the default padding is space, so this will add spaces to the left.

    stream << setw(13) << yourString
    

    See: setw(). You'll need to include <iomanip>.

    0 讨论(0)
提交回复
热议问题