Sunday, April 21, 2024

C++ Manipulator:



Controllers are extraordinary capabilities that can be remembered for the I/O explanation to change the configuration boundaries of a stream. Controllers are administrators that are utilized to design the information show. To get to controllers, the record iomanip. h ought to be remembered for the program.

Manipulators are assisting features that could modify the enter/output circulation. It does not suggest that we exchange the value of a variable, it most effective modifies the I/O movement the usage of insertion (<<) and extraction (>>) operators. 

Manipulators are unique capabilities that may be included in the I/O announcement to adjust the layout parameters of a circulate. 

Manipulators are operators that are used to format the statistics display.

To get entry to manipulators, the document iomanip.H should be covered within the software.

For instance, if we need to print the hexadecimal value of a hundred then we are able to print it as:

cout<<setbase(sixteen)<<one hundred

Types of Manipulators: There are various styles of manipulators:

1.Manipulators without arguments: The most crucial manipulators defined by means of the IOStream library are furnished underneath.

Endl: It is described in ostream. It is used to enter a brand new line and after entering a brand new line it flushes (i.E. It forces all of the output written on the screen or inside the record) the output move.

 It is defined in iostream and is used to disregard the whitespaces in the string series.

Ends: It is also defined in iostream and it inserts a null individual into the output circulate. It normally works with std::iostrstream, when the related output buffer desires to be null-terminated to be processed as a C string.

Flush: It is also defined in iostream and it flushes the output circulation, i.e. It forces all the output written on the display or within the report. Without flush, the output would be the equal, but may not seem in real-time. 

Examples:
#include <iostream>
#include <iostream>
#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    istringstream str("           Programmer");
    string line;
    // Ignore all the whitespace in string
    // str before the first word.
    getline(str >> std::ws, line);
 
    // you can also write str>>ws
    // After printing the output it will automatically
    // write a new line in the output stream.
    cout << line << endl;
 
    // without flush, the output will be the same.
    cout << "only a test" << flush;
 
    // Use of ends Manipulator
    cout << "\na";
 
    // NULL character will be added in the Output
    cout << "b" << ends;
    cout << "c" << endl;
 
    return 0;
}
Output:
Programmer
only a test
abc
  1. Manipulators with Arguments: Some of the manipulators are used with the argument like setw (20), setfill (‘*’), and many more. These all are defined in the header file. If we want to use these manipulators then we must include this header file in our program. For Example, you can use following manipulators to set minimum width and fill the empty space with any character you want: std::cout << std::setw (6) << std::setfill (’*’);
    • Some important manipulators in <iomanip> are:
      1. setw (val): It is used to set the field width in output operations.
      2. setfill (c): It is used to fill the character ‘c’ on output stream.
      3. setprecision (val): It sets val as the new value for the precision of floating-point values.
      4. setbase(val): It is used to set the numeric base value for numeric values.
      5. setiosflags(flag): It is used to set the format flags specified by parameter mask.
      6. resetiosflags(m): It is used to reset the format flags specified by parameter mask.
    • Some important manipulators in <ios> are:
      1. showpos: It forces to show a positive sign on positive numbers.
      2. noshowpos: It forces not to write a positive sign on positive numbers.
      3. showbase: It indicates the numeric base of numeric values.
      4. uppercase: It forces uppercase letters for numeric values.
      5. nouppercase: It forces lowercase letters for numeric values.
      6. fixed: It uses decimal notation for floating-point values.
      7. scientific: It uses scientific floating-point notation.
      8. hex: Read and write hexadecimal values for integers and it works same as the setbase(16).
      9. dec: Read and write decimal values for integers i.e. setbase(10).
      10. oct: Read and write octal values for integers i.e. setbase(10).
      11. left: It adjusts output to the left.
      12. right: It adjusts output to the right.

Types of Manipulators:

There are two types of manipulators used generally:
1] Parameterized and 
2] Non-parameterized

No comments:

Post a Comment

last post

C language Vs C++ language in 2024

Popular Posts