Effective Use Of C++ Iomanip Thư Viện, Effective Use Of C++ Iomanip Library

In C++, the header file defines the manipulator functions that are used to lớn manipulate the format of the input and output of our program. It is a part of the input/output library in C++. In this article, we will discuss the functions & facilities provided in the header file in C++ with examples.

Bạn đang xem: Iomanip thư viện

C++ Functions

The following table lists the commonly used functions:


S. No.Function

Description

Syntax

1resetiosflags2setiosflags3

setbase

4

setfill

5

setprecision


6

setw

7

get_money

8

put_money

9

get_time

10

put_time

11

quoted

This function is used lớn reset or clear ios_base flags specified by the parameter mask.resetiosflags (ios_base :: fmtflags mask);
This function is used to set the ios_base flags specified by the parameter mask.setiosflags(ios_base :: fmtflags mask);
This function is used khổng lồ set the base according lớn the argument specified as a parameter lớn the function.setbase(int base);
This function is used to lớn change the fill character. It is used to set the fill according lớn the character specified as a parameter lớn the function.setfill(char c);
This function is used lớn change the floating-point precision based on the precision specified as a parameter lớn the function.setprecision(int n);
This function is used to change the width of the next input/output field based on the width specified as a parameter khổng lồ the function.setw(int n);
This function is used to lớn parse a monetary value.get_money(money
T& mon, bool intl = false);
This function is used to lớn format & output a monetary value.put_money(const money
T& mon, bool intl = false);
This function is used to parse a date/time value according to lớn the specified format.get_time(tm* tmb, const char
T* fmt);
This function is used lớn format và output a date/time value according khổng lồ the format specified.put_time(const tm* tmb, const char
T* fmt);
This function is used khổng lồ insert & extract quoted strings with embedded space.quoted(const Char
T* s, Char
T delim = Char
T(‘ ” ‘), Char
T escape = Char
T(‘\’));

Lets discuss each of these functions one by one.

Xem thêm: Thư Viện Sketchup Là Gì ? 5 Ưu Điểm Sketchup Dân Kiến Trúc Cần Phải Biết

1. setiosflags in C++

The setiosflag function is used to set the ios_base flags that are specified by its parameter for the stream it is used on.

Many candidates are rejected or down-leveled due to lớn poor performance in their System thiết kế Interview. Stand out in System thiết kế Interviews & get hired in 2024 with this popular free course.


iomanip is a library in C++ that is used khổng lồ manage input and output formatting. It gives the user the power to control output formatting processes, such as alignment, text formatting, và numeric precision in the console for better data visualization. In this Answer, we will explain several functions (illustrated below) along with their code for better understanding.


span>*>:object-cover ">
*

The Setw function

Setw stands for "set width". This function allows the user to lớn define the minimum width of the following output field in a stream. It aligns data in columns & has a well-organized output to read. Setw takes int as an input.


By running the above code, you understand how it beautifies the output by adding spaces. If we don't use setw, it will result in a wrong impression to lớn the user.

The setprecision function

This function sets the precision or the number of decimal places khổng lồ be displayed when outputting a floating-point number. It can also round off the last displayed digit it needs. The parameter defines how many digits are khổng lồ be displayed in the output. You can run this function by changing the parameter passed to setprecision(). This function is mainly used in displaying significant digits.


The fixed function

To display the decimal number in fixed-point notation, we use the fixed function provided in the iomanip library. The primary purpose of the fixed function is to enforce a consistent number of decimal places irrespective of their actual precision.


The scientific function

In scientific notation, we can express a number that is too large or small in the nguồn of 10. Iomanip has the scientific function that converts an đầu ra field into a readable form in the nguồn of 10.

Below is the code that shows how the scientific function converts a number khổng lồ an exponential khung so the user can read & interpret it easily.


The left function

The left manipulator in iomanip sets the output đầu ra of the field lớn the left side & displays any additional width specified by the setw as spaces lớn the right. You can run và test the below code by passing different parameters to lớn the setw function so that you can analyze the working of the left operation.


The right function

The right function sets the output field of the text to lớn the right side by adding spaces lớn the left as defined by the setw. Have a look at its code.


The setfill function

The setfill function is used khổng lồ set the fill character when the width of the content displayed is smaller than the minimum defined by the setw. It helps in creating a visually consistent & organized output in the console. You can pass any character lớn the setfill function. In the below code, we pass the character '0' in it. By default, the default character is a space ( ' ') .


Conclusion

the iomanip library in C++ is like a secret toolkit that adds magic to lớn formatting và displaying data. It is the ultimate ally of developers that provides flexible manipulators and functions lớn fine-tune the appearance of output streams. With iomanip, programmers can effortlessly create eye-catching displays that make their applications user-friendly, polished, and professional.


Which manipulator is used to set the width of the next output đầu ra field to lớn be exactly n characters wide?

Leave a Reply

Your email address will not be published. Required fields are marked *

x

Welcome Back!

Login to your account below

Retrieve your password

Please enter your username or email address to reset your password.