The C++ manipulator right function is employed to establish the adjustfield format flag for the str stream to right.
When we specify adjustfield as right, the result gets aligned to the right by adding fill characters at the start, thus adjusting the field towards the right side.
Syntax
ios_base& left (ios_base& str);
Parameter
str: stream object whose format flag is affected.
Return value
It returns argument str.
Data Races
Data races can occur when multiple threads concurrently access and modify the same stream object.
Exceptions
str is considered to be in a valid state even if an exception is raised.
Example 1
Let's explore a basic example to illustrate the utilization of the right manipulator:
#include <iostream> // std::cout, std::internal, std::left, std::right
using namespace std;
int main () {
int n = -24;
cout.width(6);
cout << internal << n << '\n';
cout.width(6);
cout << left << n << '\n';
cout.width(6);
cout << right << n << '\n';
return 0;
}
Output:
- 24
-24
-24
Example 2
Let's see another simple example:
#include <iostream>
int main( )
{
using namespace std;
double f1= 5.00;
cout << f1 << endl;
cout.width( 20 );
cout << f1 << endl;
cout.width( 20 );
cout << left << f1 << endl;
cout.width( 20 );
cout << f1 << endl;
cout.width( 20 );
cout << right << f1 << endl;
cout.width( 20 );
cout << f1 << endl;
return 0;
}
Output:
5
5
5
5
5
5
Example 3
Let's see another simple example:
#include <iostream>
#include <iomanip>
#include <locale>
using namespace std;
int main()
{
cout.imbue(locale("en_US.utf8"));
cout << "Left fill:\n" << left << setfill('*')
<< setw(12) << -1.23 << '\n'
<< setw(12) << hex << showbase << 42 << '\n'
<< setw(12) << put_money(123, true) << "\n\n";
cout << "Internal fill:\n" << internal
<< setw(12) << -1.23 << '\n'
<< setw(12) << 42 << '\n'
<< setw(12) << put_money(123, true) << "\n\n";
cout << "Right fill:\n" << right
<< setw(12) << -1.23 << '\n'
<< setw(12) << 42 << '\n'
<< setw(12) << put_money(123, true) << '\n';
return 0;
}
Output:
Left fill:
-1.23*******
0x2a********
USD *1.23***
Internal fill:
-*******1.23
0x********2a
USD ****1.23
Right fill:
*******-1.23
********0x2a
***USD *1.23