std::ws

std::ws (공백 건너뛰기 조작자)

입력 스트림에서 선행 공백을 버리는 조작자예요. UnformattedInputFunction처럼 동작하되 is.gcount()는 수정하지 않아요.

출처: cppreference

본문

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

template< class CharT, class Traits >
std::basic_istream<CharT, Traits>& ws( std::basic_istream<CharT, Traits>& is );

입력 스트림에서 선행 공백을 버려요. UnformattedInputFunction처럼 동작하되 is.gcount()는 수정하지 않아요. sentry 객체를 생성·검사한 뒤, 다음 조건 중 하나가 발생할 때까지 스트림에서 문자를 추출해 버려요.

  • 입력 시퀀스에서 end-of-file 조건 발생(이 경우 setstate(eofbit) 호출, failbit는 설정하지 않음).
  • 다음 사용 가능한 문자 cstd::isspace(c, is.getloc())에 따라 공백이 아님(이 경우 c는 넣지 않음).

예제

#include <iostream>
#include <sstream>

int main()
{
    std::istringstream s("   hello  world");
    std::string a, b;
    s >> a;    // "hello" (표준 형식화 입력은 이미 공백을 건너뜀)
    s >> std::ws >> b;  // 명시적으로 공백 건너뛰기, "world"
    std::cout << a << ' ' << b << '\n';
}

더 알아보기 (Learn more)

cppreference