C++은 출력포맷을 맞춰주기 위해 다양한 포맷 입출력 기능을 제공한다.
ios 클래스에 포맷 플래그가 정의되어 있다.
https://msdn.microsoft.com/ko-kr/library/5yc0df6d.aspx
Manipulators(조작자)는 다음과 같으며, 예를 들어 ios:fixed 와 같은 형태로 사용된다.
Specifies that variables of type bool appear as true or false in the stream. | |
Specifies that integer variables appear in base 10 notation. | |
Configures the flags of an ios_base object to use a default display format for float values. | |
Specifies that a floating-point number is displayed in fixed-decimal notation. | |
Specifies that integer variables appear in base 16 notation. | |
Causes a number's sign to be left justified and the number to be right justified. | |
Causes text that is not as wide as the output width to appear in the stream flush with the left margin. | |
Specifies that variables of type bool appear as 1 or 0 in the stream. | |
Turns off indicating the notational base in which a number is displayed. | |
Displays only the whole-number part of floating-point numbers whose fractional part is zero. | |
Causes positive numbers to not be explicitly signed. | |
Cause spaces to be read by the input stream. | |
Causes output to be buffered and processed when the buffer is full. | |
Specifies that hexadecimal digits and the exponent in scientific notation appear in lowercase. | |
Specifies that integer variables appear in base 8 notation. | |
Causes text that is not as wide as the output width to appear in the stream flush with the right margin. | |
Causes floating point numbers to be displayed using scientific notation. | |
Indicates the notational base in which a number is displayed. | |
Displays the whole-number part of a floating-point number and digits to the right of the decimal point even when the fractional part is zero. | |
Causes positive numbers to be explicitly signed. | |
Cause spaces to not be read by the input stream. | |
Causes output to be processed when the buffer is not empty. | |
Specifies that hexadecimal digits and the exponent in scientific notation appear in uppercase. |
또한 <iomanip>에는 << 와 >> 연산자와 함께 사용되는 조작자(manipulator)를 제공하는데, https://msdn.microsoft.com/ko-kr/library/ydd54a6t.aspx 다음과 같이 사용된다.
Obtains a monetary amount, optionally in international format. | |
Obtains a time in a time structure by using a specified format. | |
Provides a monetary amount, optionally in international format. | |
Provides a time in a time structure and a format string to use. | |
Enables convenient round-tripping of strings with insertion and extraction operators. | |
Clears the specified flags. | |
Set base for integers. | |
Sets the character that will be used to fill spaces in a right-justified display. | |
Sets the specified flags. | |
Sets the precision for floating-point values. | |
Specifies the width of the display field. |
예를 들어 double value 값을 소수점 아래 둘째자리까지 출력하는데, 폭을 5로 맞춘다면
cout.setf(ios::fixed);
cout << "p = " << setw(5) << setprecision(2) << value<< endl;
또는
// <iomanip> 사용
cout << fixed << setprecision(3) << "x = " << x << ", y = " << y << endl;
와 같이 사용한다.
'C++' 카테고리의 다른 글
C++ 입출력시스템 정리 (0) | 2016.07.05 |
---|---|
STL vector와 sort (0) | 2016.07.05 |
STL vector의 사용법 (0) | 2016.07.04 |
enum 과 연산자 중복의 예 (0) | 2016.07.04 |
Template를 사용한 swap 함수 (0) | 2016.07.04 |