cout

Kiwoong Park·2023년 7월 14일
0

출처 : C++ 실력 완성 올인원 패키지(fastcampus) feat. chat GPT
깃헙

활용 헤더 파일

#include <cstdio>
#include <iostream>
#include <iomanip>
using namespace std;

<< / .put() / .write()

int main() {
    cout << 'a' << 'b' << endl;         // ab
    cout.put('a').put('b').put('\n');   // ab
    cout.write("ab\n", 3);              // ab

1. cout <<:

The << operator is the stream insertion operator in C++. It is used to insert data into the output stream, cout. It supports various data types and allows you to concatenate multiple values or expressions together. It automatically converts the input data to a string representation and sends it to the output stream. Here's an example:

int num = 10;
cout << "The value of num is: " << num << endl;

Output:

The value of num is: 10

In this example, cout << "The value of num is: " inserts the string into the output stream, followed by num, which is an integer. The << operator handles the conversion of the integer to a string representation and sends it to cout.

2. cout.put():

The put() function is a member function of the std::ostream class, which is the base class of cout. It is used to output a single character to the stream. It takes an int parameter representing the character code and returns a reference to the stream. Here's an example:

char ch = 'A';
cout.put(ch);

Output:

A

In this example, cout.put(ch) outputs the character 'A' to the stream. It directly writes the character as is, without any conversion or formatting.

3. cout.write():

The write() function is a member function of the std::ostream class. It is used to output a sequence of characters to the stream. It takes two parameters: a pointer to the character array and the number of characters to write. It returns a reference to the stream. Here's an example:

const char* str = "Hello, World!";
int length = 13;
cout.write(str, length);

Output:

Hello, World!

In this example, cout.write(str, length) writes the string "Hello, World!" to the stream. It writes the specified number of characters from the provided character array.

In summary, cout << is used for general output and supports various data types, cout.put() is used to output a single character, and cout.write() is used to output a sequence of characters. Each has its own specific purpose and usage depending on the desired output.

여백 만들기

    // 10칸 여백
    cout.width(10);
    cout << 1 << endl;
    
    // 10칸을 여백
    cout.width(10);
    // 여백에 공백 문자 대신 '#'을 넣어준다.
    cout.fill('#');
    cout << 1 << endl;
    // '#'이 었던 것을 원상 복구
    cout.fill(' ');

정밀도, 양수, bool 표현, 정렬 방법

    float num = 0.1234567;
    // 정밀도 조절 없이 출력 ~ 소수점 6자리까지 반올림하여 출력
    cout << num << endl; // 0.123457
    // 정밀도 조절
    cout.precision(2);  
    cout << num << endl; // 0.12

    // 정밀도 조절
    cout.precision(64);
    cout << num << endl; // 0.12345670163631439208984375
    
    // 양수에 + 기호를 표시하게 한다.
    cout.setf(ios_base::showpos);
    cout << 1 << endl; // +1
    cout << 2 << endl; // +2

    // 양수에 + 기호 표시를 하지 않게 한다.
    cout.unsetf(ios_base::showpos);
    cout << 1 << endl;
    cout << 2 << endl;
    
    // bool 값을 true/false로 출력하게 한다.
    cout.setf(ios_base::boolalpha);
    cout << true << endl;
    cout << false << endl;

    // bool 값을 1/0으로 출력하게 한다.
    cout.unsetf(ios_base::boolalpha);
    cout << true << endl;
    cout << false << endl;
    
    // 부호와 숫자를 양 끝에 배치한다.
    cout.setf(ios_base::internal, ios_base::adjustfield);
    cout.width(10);
    cout << -10 << 20 << endl;
    
    // 왼쪽 정렬한다.
    cout.setf(ios_base::left, ios_base::adjustfield);
    cout.width(10);
    cout << -10 << 20 << endl;
    
    // 오른쪽 정렬한다.
    cout.setf(ios_base::right, ios_base::adjustfield);
    cout.width(10);
    cout << -10 << 20 << endl;

진수 표현

  // 8진수로 출력한다.
  cout.setf(ios_base::oct, ios_base::basefield);
  cout << 16 << endl; // 20

  // 16진수로 출력한다.
  cout.setf(ios_base::hex, ios_base::basefield);
  cout << 16 << endl; // 10

  // 10진수로 출력한다.
  cout.setf(ios_base::dec, ios_base::basefield);
  cout << 16 << endl; // 16
  
  // 16진수, 8진수, 10진수 출력을 다른 방식으로 표현
  cout << hex << 16 << endl; // 10
  cout << oct << 8 << endl; // 10
  cout << dec << 10 << endl; // 10

  // hex는 함수이다.
  hex(cout);
  cout << 16 << endl; // 10

  // oct는 함수이다.
  oct(cout);
  cout << 8 << endl; // 10

  // dec는 함수이다.
  dec(cout);
  cout << 10 << endl; // 10

종합 및 stream을 파일로 출력 변경하기

    // << 을 이용하여 기존에 사용했던 것들을 표현
    cout << setw(10) << setfill('#') << setprecision(2) << showpos << 1.1f;
    
    // stdout를 output.txt 파일로 출력
    freopen("output.txt", "w", stdout);
    cout << "Hello World" << endl;
    
    
}
profile
You matter, never give up

0개의 댓글