std::flush

std::flush (flush 조작자)

출력 시퀀스 osos.flush()를 호출한 것처럼 비우는(flush) 조작자예요. 출력 전용 I/O 조작자예요.

출처: cppreference

본문

<ostream> 헤더에 정의돼 있어요.

template< class CharT, class Traits >
std::basic_ostream<CharT, Traits>& flush( std::basic_ostream<CharT, Traits>& os );

출력 시퀀스 osos.flush()를 호출한 것처럼 비워요. 출력 전용 I/O 조작자라 std::basic_ostream 타입의 out에 대해 out << std::flush처럼 쓸 수 있어요.

참고

이 조작자는 불완전한 출력 줄을 즉시 만들어내는 데 쓸 수 있어요. 예를 들어 긴 실행 프로세스의 출력을 표시하거나, 여러 스레드의 활동·중단될 수 있는 프로그램을 로깅할 때 유용해요.

예제

#include <iostream>
#include <chrono>
#include <thread>

int main()
{
    for (int i = 0; i < 3; ++i)
    {
        std::cout << "진행 중 " << i << std::flush; // 즉시 출력
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
    std::cout << '\n';
}

더 알아보기 (Learn more)

cppreference