Padding stl strings in C++

std::setw (setwidth) manipulator

std::cout << std::setw (10) << 77 << std::endl;

or

std::cout << std::setw (10) << "hi!" << std::endl;

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>

Leave a Comment