C++ cout

2021. 3. 29. 23:56·C++/Library

cout

  • <iostream> 라이브러리

출력 형식 설정

  • 여러가지 방법으로 출력 형식을 설정할 수 있다.

  • +부호 붙이기, 16진수 출력, 대문자 출력

    #include <iostream>
    
    int         main()
    {
        using namespace std;
    
        cout.setf(std::ios::showpos);
        cout << 108 << endl;
    
        cout.unsetf(std::ios::showpos);
        cout << 108 << endl;
    
        cout.unsetf(std::ios::dec);
        cout.setf(std::ios::hex);
        cout << 108 << endl;
    
        cout.setf(std::ios::hex, std::ios::basefield);
        cout << 108 << endl;
    
        cout << std::dec;
        cout << 108 << endl;
    
        cout.setf(std::ios::uppercase);
        cout << std::hex;
        cout << 108 << endl;
    
        cout << std::nouppercase;
        cout << 108 << endl;
    
        cout << boolalpha << true << ' ' << false << endl;
        cout << noboolalpha << true << ' ' << false << endl;
    }
    
    

/* stdout stderr
+108
108
6c
6c
108
6C
6c
true false
1 0
*/


## `<iomanip>`

- `<iomanip>` 라이브러리를 포함하면 더 유연하게 출력 형식을 조절할 수 있다.

- 소수점 자릿수 조절, 과학적 표기법, 소수점 표시

  ```c++
  #include <iostream>
  #include <iomanip>

  void        printFloat()
  {
      using namespace std;

      cout << std::setprecision(3) << 123.456 << endl;
      cout << std::setprecision(4) << 123.456 << endl;
      cout << std::setprecision(5) << 123.456 << endl;
      cout << std::setprecision(6) << 123.456 << endl;
      cout << std::setprecision(7) << 123.456 << endl;
      cout << endl;
  }

  int         main()
  {
      using namespace std;

      printFloat();

      cout << std::fixed;
      printFloat();

      cout << std::scientific << std::uppercase;
      cout << "this is lowercase\n";
      printFloat();

      cout << std::defaultfloat;
      cout << std::showpoint;
      printFloat();
  }


  /* stdout stderr
  123
  123.5
  123.46
  123.456
  123.456

  123.456
  123.4560
  123.45600
  123.456000
  123.4560000

  this is lowercase
  1.235E+02
  1.2346E+02
  1.23456E+02
  1.234560E+02
  1.2345600E+02

  123.
  123.5
  123.46
  123.456
  123.4560
  */
  • 좌우 정렬, 공백 대신 문자 채우기

    #include <iostream>
    #include <iomanip>
    
    int         main()
    {
        using namespace std;
    
        cout << -12345 << endl;
        cout << std::setw(10) << -12345 << endl;
        cout << std::setw(10) << std::left << -12345 << endl;
        cout << std::setw(10) << std::right << -12345 << endl;
        cout << std::setw(10) << std::internal << -12345 << endl;
        cout << endl;
        cout.fill('*');
        cout << std::setw(10) << -12345 << endl;
        cout << std::setw(10) << std::left << -12345 << endl;
        cout << std::setw(10) << std::right << -12345 << endl;
        cout << std::setw(10) << std::internal << -12345 << endl;
    }
    
    

/* stdout stderr
-12345
-12345
-12345
-12345

  • 12345

  • ****12345

  • 12345****

  • ***-12345

  • ****12345

  • /
    ```

저작자표시 (새창열림)

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

C++ 정규 표현식 (Regular Expressions)  (0) 2021.03.29
C++ 흐름 상태 (Stream States)  (0) 2021.03.29
C++ ostream  (0) 2021.03.29
C++ istream  (0) 2021.03.29
C++ wstring  (0) 2021.03.26
'C++/Library' 카테고리의 다른 글
  • C++ 정규 표현식 (Regular Expressions)
  • C++ 흐름 상태 (Stream States)
  • C++ ostream
  • C++ istream
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 분류 전체보기 (317)
      • Algorithm (13)
        • 알기 쉬운 알고리즘 (10)
        • Search (1)
        • Sort (2)
      • Arduino (0)
      • C++ (185)
        • Class (46)
        • Exception (6)
        • Library (51)
        • Overloading (10)
        • SmartPointer (5)
        • Syntax (33)
        • TBC++ (23)
        • Templates (9)
        • VisualStudio (2)
      • Embedded (1)
      • Git (4)
      • Java (5)
      • Linux (16)
        • Error (1)
        • Linux Structure (11)
      • MacOS (7)
      • OS (1)
        • Concurrency (1)
      • Python (21)
        • Class (1)
        • Function (2)
        • Syntax (17)
      • Raspberrypi (9)
      • Review (1)
      • Utility (12)
        • VSCode (5)
        • VirtualBox (3)
      • Web (8)
        • Nginx (1)
        • React (3)
        • Django (1)
      • Windows (20)
        • Registry (3)
        • WSL (1)
        • DeviceDriver (6)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    java
    dism
    알림
    vscode
    그림판
    로지텍 마우스 제스처
    SFC
    Workspace
    시스템 복구
    스프링
    윈도우 명령어
    windows
    Windows 11
    spring
    제외
    KakaoTalk
    맥북 카카오톡 알림 안뜸
    mspaint
    SunOS 5.1
    백기선
    logi options
    스프링 프레임워크 핵심 기술
    citrix workspace
    unix
    윈도우
    Solaris 10
    MacOS
    EXCLUDE
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ cout
상단으로

티스토리툴바