std::ios manipulators

std::ios manipulators (I/O 조작자)

operator<< 또는 operator>>로 입력·출력 스트림을 제어하는 데 쓰는 헬퍼 함수들이에요.

출처: cppreference

본문

인자 없이 호출되는 조작자(예: std::cout << std::boolalpha; 또는 std::cin >> std::hex;)는 스트림에 대한 참조를 유일한 인자로 받는 함수로 구현돼요. basic_ostream::operator<<basic_istream::operator>>의 특수 오버로드가 이 함수들에 대한 포인터를 받아요. 이 함수들(또는 함수 템플릿의 인스턴스화)은 표준 라이브러리에서 주소를 취할 수 있는 유일한 함수들이에요(C++20부터).

인자와 함께 호출되는 조작자(예: std::cout << std::setw(10);)는 미지정 타입의 객체를 반환하는 함수로 구현돼요. 이 조작자들은 요청된 조작을 수행하는 자신만의 operator<< 또는 operator>>를 정의해요.

<ios> 헤더의 조작자

  • boolalpha / noboolalpha — 부울의 텍스트·수치 표현 전환.
  • showbase / noshowbase — 수치 진법 접두사 표시 여부.
  • showpoint / noshowpoint — 소수점 표시 여부.
  • showpos / noshowpos — 양수 부호 표시 여부.
  • skipws / noskipws — 선행 공백 건너뛰기.
  • unitbuf / nounitbuf — 출력 후 자동 flush.
  • uppercase / nouppercase — 대문자 사용.
  • dec / hex / oct — 진법 선택.
  • fixed / scientific / hexfloat / defaultfloat — 부동소수점 표기.
  • left / right / internal — 정렬.

<iomanip> 헤더의 조작자(인자 포함)

  • setw, setprecision, setfill, setbase, setiosflags, resetiosflags, quoted, get_money, put_money, get_time, put_time.

<istream>/<ostream>의 조작자

  • ws(공백 건너뛰기), endl(개행+flush), ends(null), flush, emit_on_flush/noemit_on_flush(C++20), flush_emit(C++20).

예제

#include <iostream>
#include <iomanip>

int main()
{
    std::cout << std::boolalpha << true << '\n';     // "true"
    std::cout << std::hex << 255 << '\n';            // "ff"
    std::cout << std::setw(5) << std::setfill('0') << 42 << '\n'; // "00042"
}

더 알아보기 (Learn more)

cppreference