C++ Chapter 18.2 : ostream으로 출력하기

Date:     Updated:

카테고리:

태그:

인프런에 있는 홍정모 교수님의 홍정모의 따라 하며 배우는 C++ 강의를 듣고 정리한 필기입니다. 😀
🌜 [홍정모의 따라 하며 배우는 C++]강의 들으러 가기!


chapter 18. 입력과 출력

ostream으로 출력하기

#include <iostream>

#include <iomanip>

🔔 cout.setf, cout.unsetf

  • cout.setf
    • 출력 옵션을 바꿀 수 있다.
  • cout.unsetf
    • setf로 세팅한 출력 옵션 해제

std::ios::showpo

cout.setf(std::ios::showpo) 👉 양수도 + 기호가 붙어서 나오게끔 출력한다.

#include <iostream>
#include <string>
#include <iomanip>	

using namespace std;

int main()   
{
	cout.setf(std::ios::showpos);
	cout << 108 << endl;

    cout.unsetf(std::ios::showpos);
    cout << 108 << endl;

	return 0;
}
💎출력💎

+108
108


std::ios::hex

cout.setf(std::ios::hex) 👉 16진수로 출력되게 한다.

  • 정수를 16진수로 출력하기
    • 1️⃣ 반드시 먼저 10진수 모드를 꺼준다.
      #include <iostream>
      #include <string>
      #include <iomanip>	
      
      using namespace std;
      
      int main()   
      {
          cout.unsetf(std::ios::dec);  // 먼저 10진수 모드 꺼주고 나서
          cout.setf(std::ios::hex);  // 16진수로 출력 세팅
          cout << 108 << endl;  // 108의 16진수인 "6c" 출력
      
          return 0;
      }
      
    • 2️⃣ 혹은 std::ios::basefield를 16진수로 설정해주면 된다. 따라서 10진수 모드를 끌 필요 없다.
      #include <iostream>
      #include <string>
      #include <iomanip>	
      
      using namespace std;
      
      int main()   
      {
          cout.setf(std::ios::hex, std::ios::basefield);  //  16진수로 출력하는 것을 디폴트로 세팅
          cout << 108 << endl;  // 108의 16진수인 "6c" 출력
      
          return 0;
      }
      
    • 3️⃣ coutstd::hex를 흘려보낸 후 출력하는 방법도 있다. std::hexstd::decinclude <manip>에 있다.
      #include <iostream>
      #include <string>
      #include <iomanip>	
      
      using namespace std;
      
      int main()   
      {
          cout << std::hex;   // 16진수로 출력되게끔
          cout << 108 << endl;  // 108의 16진수인 "6c" 출력
          cout << 109 << endl;  // 109의 16진수인 "6d" 출력
      
          cout << std::dec;   // 다시 십진수로 출력되게끔
          cout << 108 << endl;  // "108" 출력
          cout << 109 << endl;  // "109" 출력
      
          return 0;
      }
      


std::ios::uppercase

std::ios::uppercase 👉 16 진수에 사용되는 abcdef 알파벳을 대문자로 출력하게끔 한다. (소문자가 디폴트)

  1. cout.setf(std::ios::uppercase)
  2. 혹은 cout << std::hex << std::uppercase
    • std::uppercase 또한 include <manip>에 있다
    #include <iostream>
    #include <string>
    #include <iomanip>	

    using namespace std;

    int main()   
    {
	    cout << std::hex << std::uppercase;   // 16진수로 출력되게끔
	    cout << 108 << endl;  // 108의 16진수인 "6C" 출력
        cout << 109 << endl;  // 109의 16진수인 "6D" 출력

        cout << std::dec;   // 다시 십진수로 출력되게끔
	    cout << 108 << endl;  // "108" 출력
        cout << 109 << endl;  // "109" 출력

	    return 0;
    }


🔔 std::boolalpha

std::boolalphainclude <manip>에서 사용할 수 있으며 bool값(0, 1)을 true, false 문자열로 출력하고 싶을 때 사용하면 된다.

    #include <iostream>
    #include <string>
    #include <iomanip>	

    using namespace std;

    int main()   
    {

        cout << true << " " << false << endl;

	    cout << std::boolalpha;
        cout << true << " " << false << endl;

	    return 0;
    }
💎출력💎

1 0
true false


🔔 문자 정밀도 조절

std::setprecision

#include <iomanip>

  • 자연수를 포함하여 n개로 출력된다.
  • 반올림도 된다.
#include <iostream>
#include <string>
#include <iomanip>	

using namespace std;

int main()   
{
	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;

	return 0;
}
💎출력💎

123
123.5
123.46
123.456
123.456
  • std::setprecision(5) << 123.456
    • 123.456 을 5 자리로 출력한다.
    • 소수점 셋째 자리 6에서 반올림되어 123.46이 출력된다.


std::fixed

#include <iomanip>

  • std::setprecision(n) 앞에 써주면 소수점부터 시작하여 n개를 출력한다
    • std::setprecision(n)가 자연수를 포함하지 않고 소수부터 n개 출력되도록 해준다.
#include <iostream>
#include <string>
#include <iomanip>	

using namespace std;

int main()   
{
    cout << std::fixed;  // ⭐
	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;

	return 0;
}
💎출력💎

123.456
123.4560
123.45600
123.456000
123.4560000


std::showpoint

#include <iomanip>

  • std::setprecision(n) 앞에 써주면 소수점도 `.`도 출력되게끔 한다.
    • 123 같은 정수도 123. 으로 소수점 붙여 출력되게끔
#include <iostream>
#include <string>
#include <iomanip>	

using namespace std;

int main()   
{
    cout << std::showpoint;
	cout << std::setprecision(3) << 123.456 << endl;
	
	return 0;
}
💎출력💎0

123.


🔔 문자의 폭, 맞춤 지정

#include <iomanip>

#include <iostream>
#include <string>
#include <iomanip>	

using namespace std;

int main()   
{
    cout << -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.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;
	
	return 0;
}
💎출력💎

-12345
-12345
    -12345
-    12345
-****12345
-12345****
****-12345
-****12345

std::sew

  • std::setw(10)
    • 폭을 10의 크기로 지정하고 10 글자만 출력되게끔 한다.
    • 디폴트로 오른쪽부터 문자가 들어간다.

std::left

  • 왼쪽 정렬

std::right

  • 오른쪽 정렬

std::internal

  • 부호는 왼쪽 정렬 / 숫자는 오른쪽 정렬
    • std::setw(10) << std::internal << -12345- 12345가 된다.

cout.fill

  • 빈칸을 해당 문자로 채워준다. cout.fill(*) 빈칸을 별 *로 채워줌.


🌜 개인 공부 기록용 블로그입니다. 오류나 틀린 부분이 있을 경우 
언제든지 댓글 혹은 메일로 지적해주시면 감사하겠습니다! 😄

맨 위로 이동하기

Cpp 카테고리 내 다른 글 보러가기

댓글 남기기