Padding stl strings in C++

后端 未结 12 1852
一向
一向 2020-12-05 22:15

I\'m using std::string and need to left pad them to a given width. What is the recommended way to do this in C++?

Sample input:

123
         


        
12条回答
  •  星月不相逢
    2020-12-05 23:08

    The easiest way I can think of would be with a stringstream:

    string foo = "foo";
    stringstream ss;
    ss << setw(10) << foo;
    foo = ss.str();
    

    foo should now be padded.

提交回复
热议问题