티스토리 뷰

언어/C++

18.2 ostream으로 출력하기

js0331 2020. 5. 25. 21:45
#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
	cout.setf(std::ios::showpos);
	cout << 108 << endl;
	
	cout.unsetf(std::ios::showpos);
	cout << 109 << endl;
	
	// 10진수 모드를 끄고 16진수 모드를 해줘야함.
	cout.unsetf(std::ios::dec);
	cout.setf(std::ios::hex);
	cout << 109 << endl;
	
	return 0;
}
+108
109
6d

 

16진수로 변환하는 방법

	// 10진수 모드를 끄고 16진수 모드를 해줘야함.
	cout.unsetf(std::ios::dec);
	cout.setf(std::ios::hex);
	cout << 109 << endl;
	
	cout.setf(std::ios::hex, std::ios::basefield);
	cout << 108 << endl;
	
	cout << std::hex;
	cout << 108 << endl;
    
    //6d
    //6c
    //6c

 

cout << std::hex;가 사용 가능한 이유는 #include<iomanip> 덕분이다.

 

 

#include<iostream>
#include<iomanip>

using namespace std;

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

 

 

#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
	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;
	
	return 0;
}
-12345
    -12345
-12345
    -12345
-    12345

 

 

만일 cout.fill('*') 코드를 추가하게 된다면?

	cout << -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;
-12345
****-12345
-12345****
****-12345
-****12345
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
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
글 보관함