C++ 입출력(1)

flowjiyun·2022년 6월 21일

CPP

목록 보기
1/4
post-thumbnail

해당 내용은 Pope 님의 C++ 언메니지드 프로그래밍 강좌를 공부하면서 정리하였습니다.

네임 스페이스

  • 네임 스페이스가 다르면 같은 함수명을 만들 수 있음
//오류가 발생하는 코드

// hello.h
void SayHello();
// hello2.h
void SayHello();

//main.cpp
#include "hello.h"
#include "hello2.h"

SayHello(); // 컴파일 오류 발생

// 올바른 예
namespace hello
{
	void PrintHelloWorld();
}

namespace hi
{
	void PrintHelloWorld();
}

hello::PrintHelloWorld();
hi::PrintHelloWorld();

using 지시문

  • 타이핑의 양을 줄이는 방법
#include <iostream>

using namespace std; // std를 namespace 기본으로 사용하겠다!

int main(void)
{
	cout << "hello, world" << endl;
	return 0;
}

출력 형식 지정(Output Formatting)

  • manipulator(조정자)를 사용하여 출력 형식을 지정할 수 있음
  • 조정자를 사용한 출력 형식 지정

int number = 123;

//양수에 플러스 사인 보여주기
cout << showpos << number; // +123
cout << noshowpos << number; // 123

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

//uppercase/nouppercase
cout << uppercase << hex << number; //7B
cout << nouppercase << hex << number; //7b

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

//left/internal/right
cout << setw(6) << left << number;    // |-123   |
cout << setw(6) << internal << number;// |-   123|
cout << setw(6) << right << number;   // |   -123|

float decimal1 = 100.0;
float decimal2 = 100.12;

//showpoint/noshowpoint

cout << noshowpoint << decimal1 << " " << decimal2; // 100 100.12
cout << showpoint << decimal1 << " " << decimal2;   // 100.000 100.120

float number = 123.456789;

//fixed/scientific
cout << fixed << number; //123.456789
cout << scientific << number; //1.2345678E+02

//boolalpha/noboolalpha
cout << boolalpha << bReady; // true
cout << noboolalpha << bReady; // 1

#inlcude <iomanip> // 해당 헤더 있어야 setw()사용 가능

//setw()
cout << setw(5) << numnber; // |  123|

//setfill()
cout << setfill('*') << setw(5) << number; // |**123|

//setprecision()
float number = 123.456789;
cout << setprecision(7) << number; // 123.4567 : 유효숫자 정하는 조정자
profile
FLOW & VECTOR

0개의 댓글