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
pad to 10 characters.
Sample output:
123
(7 spaces in front of 123)
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
pad to 10 characters.
Sample output:
123
(7 spaces in front of 123)
std::setw (setwidth) manipulator
or
outputs padded 77 and "hi!".
if you need result as string use instance of std::stringstream instead std::cout object.
ps: responsible header file
<iomanip>
You can use it like this:
you can create a string containing N spaces by calling
So you could do like this:
How about: