'프로그래밍/C++'에 해당되는 글 2건

  1. 2019.01.02 C++ 조정자(Manipulator)
  2. 2019.01.02 C++ namespace/using

C++ standard output에 대해서 알아보겠습니다.


조정자(Manipulator)

#include <iostream> 안에 있는 조정자

  • showbase/noshowbase
출력의 진법을 표기
  • showpos/noshowpos (pos -> positive)
양수 일때 + 표기/표기X
  • dec(decimal) 
10진수로 표기
  • hex(hexa decimal) 
16진수로 표기
  • oct(octal decimal) 
8진수로 표기
  • uppercase/noupeercase
대문자로 표기
  • left/internal/right
왼쪽 정렬 / 내부 정렬(부호는 왼쪽,숫자는 오른쪽)/ 오른쪽 정렬
  • showpoint/noshowpoint
소수점 값 표기 / 소수점 값 없으면 표기X 
ex)  showpoint 경우 10.1 -> 10.100
noshowpoint 경우 10.1 -> 10.1

  • fixed/scientific
고정적으로 표기 / 과학적으로 표기 ( 123.456789 -> 1.234568E+02 )

  • boolalpha / noboolalpha
true,false 로 표기 / 값으로 표기 (1,0)

#include <iomanip>
함수처럼 매개변수를 받음
  • setw(n)
n만큼의 공간차지


  • setfill(char형)

빈공간의 char형 문자를 채우기


  • setprecision(n)
n만큼의 유효한 소수점 표기



//실행 예제 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <iomanip>
 
using namespace std;
 
int main()
{
    int num = 1234;
 
    cout << showbase << dec << num << endl;        //1234
    cout << showbase << oct << num << endl;        //02322
    cout << showbase << hex << num << endl;        //0x4d2
 
    cout << noshowbase << dec << endl;            //초기화&줄맞춤
 
    cout << showpos << num << endl;                //+1234
    cout << noshowpos << num << endl;            //1234
    
    cout << endl;                                //줄맞춤
 
    cout << uppercase << hex << num << endl;    //4D2
    cout << nouppercase << hex << num << endl;    //4d2
    
    cout << noshowbase << dec << endl;            //초기화&줄맞춤
 
    cout << setw(6<< left << -num << endl;    //-1234
    cout << setw(6<< internal << -num << endl;//- 1234
    cout << setw(6<< right << -num << endl;    // -1234
    
    cout << endl;                                //줄맞춤
 
 
    float decimal1 = 10.0f;
    float decimal2 = 10.12f;
 
    cout << noshowpoint << decimal1 << " " << decimal2 << endl//10      10.12
    cout << showpoint << decimal1 << " " << decimal2 << endl;    //10.0000 10.1200
 
    cout << fixed << decimal2 << endl;                //10.120000
    cout << scientific << decimal2 << endl;            //1.012000e+01
 
    bool boolean = true;
 
    cout << fixed << endl;                                    //줄맞춤
 
    cout << boolalpha << boolean << endl;            //true
    cout << noboolalpha << boolean << endl;            //1
 
    cout << endl;                                    //줄맞춤
 
    cout << setfill('*'<< setw(6<< num << endl;    //**1234
 
    cout << endl;                                    //줄맞춤
 
    cout << setprecision(7<< decimal2 << endl;    //10.1199999
 
    system("pause");
 
    return 0;
}
cs


'프로그래밍 > C++' 카테고리의 다른 글

C++ namespace/using  (0) 2019.01.02
Posted by Dongkey
,
  • namespace(이름공간)
C++ 에서는 동일한 함수,변수의 충돌을 막기 위해 이름공간이 존재합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
 
namespace Test1
{
    void Func() {}
}
 
namespace Test2
{
    void Func() {}
}
 
int main()
{
    Test1::Func();
    Test2::Func();
    return 0;
}
cs


다음과 같이 이름공간을 부여해 함수의 충돌을 막을 수 있는 코드를 작성할 수 있습니다.


  • using
이름공간을 사용함으로써 타이핑을 줄일 수 있습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
 
using namespace Test1;
//using namespace Test2;
 
namespace Test1
{
    void Func() {}
}
 
namespace Test2
{
    void Func() {}
}
 
int main()
{
    Func();            //Test1::Func();
    Test1::Func();
    Test2::Func();
    return 0;
}
cs


하지만 4번째 줄의 주석을 풀면 Func() 함수 모호함이 생기기 때문에 에러가 발생할 수 있으니

조심히 다뤄서 사용하시면 좋겠습니다.

'프로그래밍 > C++' 카테고리의 다른 글

C++ 조정자(Manipulator)  (0) 2019.01.02
Posted by Dongkey
,