+ 1
Setfill in c++
What does setfill in c++ do? Should we always use it with setw?
7 ответов
+ 4
Armina
It is a manipulator that is used to specify the character to be used as padding when the value/object to be printed isn't wide enough to fill up the width specified by `std::setw` manipulator.
Run this code to see the difference, where padding is added to the left or right.
#include <iostream>
#include <iomanip>
int main()
{
std::cout << "fill left padding\n";
std::cout << std::setfill('*')
<< std::setw(7)
<< 2019;
std::cout << "\nfill right padding\n";
std::cout << std::left
<< std::setfill('*')
<< std::setw(7)
<< 2019;
return 0;
}
+ 3
Armina
`std::setfill` is meant to be used in tandem with `std::setw`. One defines the padding character, and the other defines the desired width (in characters) for the output.
Seems to me, `std::setfill` doesn't effect without `std::setw`. I didn't understand your idea with the '\t' tab character, can you show me in a code please?
+ 2
Armina
The tab character '\t' does appear as if it pushes the next data deeper this way. But then we can see that the specified padding character '*' is not being used to pad the output. So I guess they don't get along that well : )
+ 2
No problem Armina
I'm glad if it helps 👍
+ 1
Ipang
Thanks , is setfill used always after setw or it can be used after any spaces like"\t"?
+ 1
#include <iostream >
#include <iomanip >
using namespace std;
int main()
{
cout<<"\t"<<setfill('*')<<2019<< endl;
return 0;
}
I mean instead of using setw for spaces, use " \t "
+ 1
Thanks Ipang, you helped me alot