C++怎么输出固定位数的整数?

就是如果不够位数就能输出空格占位的。就像C里的%5d那种的?
2025-05-07 08:53:02
推荐回答(2个)
回答1:

可以用setw()函数:

//---------------------------------------------------------------------------
#include
#include

using namespace std;
int main(void)
{
cout<
return 0;
}
//---------------------------------------------------------------------------

在C++中也可以用C的库函数:

#include

using namespace std;

int main(void)
{
printf("%5d",11);
return 0;
}

回答2:

C++里难道不可以用%5d这样的了?