Namespaces
Variants

연산자>>(std::basic_istream)

cppreference.net에서
 
 
 
 
헤더 <istream>
template< class CharT, class Traits >
basic_istream<CharT, Traits>&
    operator>>( basic_istream<CharT, Traits>& st, CharT& ch );

template< class Traits >
basic_istream<char, Traits>&
    operator>>( basic_istream<char, Traits>& st, signed char& ch );

template< class Traits >
basic_istream<char, Traits>&
    operator>>( basic_istream<char, Traits>& st, unsigned char& ch );
에 정의됨 (1)
template< class CharT, class Traits>
basic_istream<CharT, Traits>&
    operator>>( basic_istream<CharT, Traits>& st, CharT* s );

template< class Traits >
basic_istream<char, Traits>&
    operator>>( basic_istream<char, Traits>& st, signed char* s );

template< class Traits >
basic_istream<char, Traits>&
    operator>>( basic_istream<char, Traits>& st, unsigned char* s );
(2) (C++20까지)
template< class CharT, class Traits, std::size_t N >
basic_istream<CharT, Traits>&
    operator>>( basic_istream<CharT, Traits>& st, CharT (&s)[N] );

template< class Traits, std::size_t N >
basic_istream<char, Traits>&
    operator>>( basic_istream<char, Traits>& st, signed char (&s)[N] );

template< class Traits, std::size_t N >
basic_istream<char, Traits>&
    operator>>( basic_istream<char, Traits>& st, unsigned char (&s)[N] );
(C++20부터)
template< class Istream, class T >
Istream&&
    operator>>( Istream&& st, T&& value );
(3) (C++11부터)
1,2) 문자 입력 연산을 수행합니다.
1) FormattedInputFunction처럼 동작합니다. 선행 공백을 건너뛸 수 있는 센트리 객체를 생성하고 확인한 후, 문자를 추출하여 ch에 저장합니다. 사용 가능한 문자가 없으면 failbit를 설정합니다 (eofbit는 FormattedInputFunction에서 요구하는 대로 추가로 설정됩니다).
2) FormattedInputFunction처럼 동작합니다. 선행 공백을 건너뛸 수 있는 센트리 객체를 생성하고 확인한 후, 연속된 문자를 추출하여 첫 번째 요소가 이 가리키는 문자 배열의 연속된 위치(C++20까지) s에 저장합니다. 다음 조건 중 하나라도 충족되면 추출이 중단됩니다:
  • 공백 문자 (ctype<CharT> 패싯에 의해 결정됨)가 발견됩니다. 공백 문자는 추출되지 않습니다.
  • st.width()이 0보다 크면 st.width() - 1개의 문자가 저장됩니다.
(C++20까지)
  • n - 1개의 문자가 저장됩니다. 여기서 n은 다음과 같이 정의됩니다:
  • st.width()이 0보다 크면 std::min(std::size_t(st.width()), N);
  • 그렇지 않으면 n은 N입니다.
(C++20부터)
  • 입력 시퀀스에 파일 끝이 발생합니다 (이 역시 eofbit를 설정합니다).
어느 경우든 출력 끝에 널 문자 값 CharT()이 추가로 저장됩니다. 추출된 문자가 없으면 failbit을 설정합니다 (널 문자는 여전히 출력의 첫 번째 위치에 기록됩니다). 마지막으로 st.width(0)를 호출하여 std::setw의 효과를 취소합니다(있는 경우).
3) 입력 스트림 객체에 대한 오른값 참조가 주어지면 적절한 추출 연산자를 호출합니다 (st >> std::forward<T>(value)와 동일). 이 오버로드는 st >> std::forward<T>(value)이 유효한 형식이고 Istream이 std::ios_base에서 공개적이고 명확하게 파생된 클래스 타입인 경우에만 오버로드 해결에 참여합니다.

참고 사항

스트림의 마지막 문자인 단일 문자를 추출하는 것은 eofbit 를 설정하지 않습니다: 이는 operator >> 를 사용하여 마지막 정수를 추출하는 것과 같은 다른 형식화된 입력 함수들과는 다르지만, 이 동작은 "%c" 형식 지정자를 사용한 std::scanf 의 동작과 일치합니다.

매개변수

st - 데이터를 추출할 입력 스트림
ch - 추출된 문자를 저장할 문자에 대한 참조
s - 포인터 (C++20 이전) 추출된 문자들을 저장할 문자 배열

반환값

1,2) st
3) std :: move ( st )

예제

#include <iomanip>
#include <iostream>
#include <sstream>
int main()
{
    std::string input = "n greetings";
    std::istringstream stream(input);
    char c;
    const int MAX = 6;
    char cstr[MAX];
    stream >> c >> std::setw(MAX) >> cstr;
    std::cout << "c = " << c << '\n'
              << "cstr = " << cstr << '\n';
    double f;
    std::istringstream("1.23") >> f; // rvalue stream extraction
    std::cout << "f = " << f << '\n';
}

출력:

c = n
cstr = greet
f = 1.23

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 수정된 동작
LWG 13 C++98 n 정의에 존재하지 않는 이름 eos 가 언급됨 CharT ( ) 로 대체됨
LWG 68 C++98 오버로드 (2)에 대해 출력 끝에 널 문자가 저장되지 않음 널 문자를 저장함
LWG 1203 C++98 rvalue 스트림에 대한 오버로드가 기본 클래스에 대한 lvalue 참조를 반환함 파생 클래스에 대한 rvalue 참조를 반환함
LWG 2328 C++98 rvalue 스트림에 대한 오버로드가 다른 인수를 lvalue로 요구함 rvalue를 허용하도록 수정됨
LWG 2534 C++98 rvalue 스트림에 대한 오버로드가 제약되지 않음 제약됨

참고 항목

형식화된 데이터를 추출합니다
(public member function)