C++ Manipulator Right Function - C++ Programming Tutorial
C++ Course / Functions / C++ Manipulator Right Function

C++ Manipulator Right Function

BLUF: Mastering C++ Manipulator Right Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ Manipulator Right Function

C++ is renowned for its efficiency. Learn how C++ Manipulator Right Function enables low-level control and high-performance computing in the tutorial below.

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

Example

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:

Example

#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:

Output

-   24

-24   

   -24

Example 2

Let's see another simple example:

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:

Output

5

                   5

5                   

5                   

                   5

                   5

Example 3

Let's see another simple example:

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:

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

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience