Cpp, std::ws
std::ws - cppreference.com
Discards leading whitespace from an input stream. Behaves as an UnformattedInputFunction, except that is.gcount() is not modified. After constructing and checking the sentry object, extracts characters from the stream and discards them until any one of the
en.cppreference.com
Defined in header 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로 설정하진 않는다; 이미 설정되어 있는 eofbit가 ws에게 호출 하는 것보다 앞서 있다면, 이는 적용하지 않는다. 이 경우 sentry 객체의 생성은 failbit로 설정된다).
- 입력 시퀀스의 다음 접근 가능한 문자가 std::isspace(c, is.getloc())으로 판단된 공백 문자가 아니다. 공맥이 아닌 문자는 추출되지 않는다.
이것은 입력 한정 I/O 조작자이다. 이것은 어떠한 std::basic_istream 타입을 사용한 in>>std::ws 와 같은 구문 표현과 함께 호출될 것이다.
Parameters
is - 입력 스트림의 참조
Return value
is( 연이은 공백의 추출 이후 스트림의 참조)
Notes
만약 eofbit가 호출에 앞서 스트림에 설정되어있다면, sentry 객체의 생성은 failbit가 설정될 것이다.
예제
#include <iostream>
#include <iomanip>
#include <istream>
#include <sstream>
#include <string>
int main()
{
for (const char* str: {" #1 test", "\t #2 test", "#3 test"}) {
std::string line;
std::getline(std::istringstream{str}, line);
std::cout << "getline returns: \t" << quoted(line) << '\n';
std::istringstream iss{str};
std::getline(iss >> std::ws, line);
std::cout << "ws + getline returns: \t" << quoted(line) << '\n';
}
}