The C++ std:ios::fill() function is used to set the character that fills the unused spaces when formatted output operations are performed. It is useful when aligning text in output streams, ensuring consistent spacing.
SyntaxBy default, the fill character is a space, but it can be customized by using the fill().
Following is the syntax for std::ios::fill() function.
char fill() const; char fill (char fillch);Parameters
It returns the value of the fill character before the call.
ExceptionsIf an exception is thrown, the stream is in a valid state.
Data racesAccesses or modifies the stream object.
Concurrent access to the same stream object may cause data races.
ExampleIn the following example, we are going to consider the basic usage of the fill() function.
#include <iostream> #include <iomanip> int main() { std::cout << "Default Fill: "; std::cout << std::setw(5) << 123 << std::endl; std::cout << "Custom Fill: "; std::cout.fill('*'); std::cout << std::setw(6) << 121 << std::endl; return 0; }Output
Output of the above code is as follows −
Default Fill: 123 Custom Fill: ***121Example
Consider the following example, where we are going to fill it with different characters.
#include <iostream> #include <iomanip> int main() { std::cout.fill('*'); std::cout << std::setw(9) << 111 << std::endl; std::cout.fill('^'); std::cout << std::setw(7) << 222 << std::endl; std::cout.fill('-'); std::cout << std::setw(5) << 333 << std::endl; return 0; }Output
Following is the output of the above code −
******111 ^^^^222 --333Example
Let's look at the following example, where we are going to use the fill() with the string.
#include <iostream> #include <iomanip> int main() { std::string a = "TutorialsPoint"; std::cout << "Default Fill: " << std::setw(17) << a << std::endl; std::cout.fill('*'); std::cout << "Custom Fill: " << std::setw(17) << a << std::endl; return 0; }Output
If we run the above code it will generate the following output −
Default Fill: TutorialsPoint Custom Fill: ***TutorialsPoint
ios.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4