728x90

1.<<연산자

inserstion? 밀어넣기? 출력, 푸쉬 연산자?

+,-등과 같은 연산자 중 하나

c++에서는 프로그래머가 연산자 동작을 바꿀 수 있다 => 연산자 오버로딩

 

2.출력형식 지정 (Output formatting)

C vs C++

 

C

서식문자를 이용하여 출력서식 지정

 int num =10;
 printf("%#x\n", number);

 

C++

manipulator를 이용한 출력서식 지정

int number = 10;
cout << showbase << hex << number << endl;

 

C++의 Manipulator(조정자) 종류

* 조정자를 이용하여 출력서식 지정시 설정된 값이 이후 출력에도 적용되는 경우가 있으므로

출력서식을 기본값으로 바꿔놓는 걸 유념하기

 

기본 조정자

 

showpos/noshowpos

-pos는 positive를 의미하며 양수 기호를 출력할지 여부를 결정합니다.

int number = 123;
cout << showpos << number << endl;    //+123
cout << noshowpos << number << endl;  //123

number = -123;
cout << showpos << number << endl; //-123
cout << noshowpos << number << endl; //-123

 

dec/hex/oct

- 숫자 출력 진법을 지정합니다.

int number = 123;
cout << dec << number << endl; //123
cout << hex << number << endl; //7b
cout << oct << number << endl; //173

 

uppercase/nouppercase

- 대소 출력을 지정합니다(문자에는 지정되지 않습니다.)

int number = 123;
cout << uppercase << hex << number << endl; //7B
cout << nouppercase << hex << number << endl; //7b
char c = 'A';
cout << nouppercase << c << endl; // A ---- 문자에는 적용되지 않음

 

showbase/noshowbase

- 진법 base 출력을 지정합니다.

int number = 123;
cout << showbase << hex << number << endl; //0x7b
cout << noshowbase << hex << number << endl; //7b

 

showpoint/noshowpoint

- 유효한 소수점 자리까지만 출력할지 여부를 지정합니다.

float decimal1 = 100.0f;
float decmall2 = 100.12f;
cout << noshowpoint << decimal1 << " " << decmall2 << endl; //100 100.12
cout << showpoint << decimal1 << " " << decmall2 << endl; //100.000 100.120

 

fixed/scientific

- 소수점 표기 방법을 구분 합니다.

float number = 123.3456789f;
cout << fixed << number << endl; //123.345680
cout << scientific << number << endl; //1.233456e+02

 

boolalpha/noboolalpha

- boolean자료형 출력 형태를 지정합니다.

bool bReady = true; 
cout << boolalpha << bReady << endl; //true
cout << noboolalpha << bReady << endl; //1

 

iomanip.h에 정의되어 있는 조정자

setw()

- 정렬 형태를 지정합니다.

int number = -123;
cout << setw(6) << left << number << endl; //[-123  ]
cout << setw(6) << internal << number << endl; //[-  123]
cout << setw(6) << right << number << endl; //[  -123]

 

setfill()

- 빈공간을 지정한 문자로 채운다.

int number = 123;
cout << setfill('*') << setw(5) << number << endl; //[**123]

 

setprecision()

- 유효숫자 정확도 수준을 지정.

float number = 123.456789f;
cout << setprecision(7) << number << endl; //123.4568
cout << setprecision(10) << number << endl; //123.4567871
728x90

'Programming Language > C++' 카테고리의 다른 글

함수 객체(함수자, Functor)  (0) 2022.02.06
함수 포인터  (0) 2022.02.06
C++/StackPadding  (0) 2021.11.23
c/c++/size_t,intptr_t,uintptr_t  (0) 2021.11.23
C++/함수호출규약/__cdecl  (0) 2021.11.23

+ Recent posts