C++ cin

2021. 3. 11. 16:34·C++/Library

cin

현재 내가 사용하는 윈도우 컴퓨터 기준으로 작성했다.

상속 관계

  • cin 은 istream 라이브러리 내부 basic_istream 클래스의 객체이다.

  • basic_istream 클래스는 ios 라이브러리 내부 basic_ios 클래스를 상속받는다.

  • basic_ios 클래스는 xiosbase 라이브러리 내부 ios_base 클래스를 상속받는다.

  • ios_base 클래스는 _Iosb 클래스를 상속받는다.

    #define _CRTIMP2_IMPORT __declspec(dllimport)
    #define _CRTDATA2_IMPORT _CRTIMP2_IMPORT
    
    class _CRTIMP2_PURE_IMPORT ios_base : public _Iosb<int>;
    class basic_ios : public ios_base;
    class basic_istream : virtual public basic_ios<_Elem, _Traits>;
    using istream       = basic_istream<char, char_traits<char>>;
    __PURE_APPDOMAIN_GLOBAL extern _CRTDATA2_IMPORT istream cin;

_Iosb

  • 기본적인 비트마스크들이 설정되어있다.

    <xiosbase>

    static constexpr _Iostate goodbit = static_cast<_Iostate>(0x0);
    static constexpr _Iostate eofbit  = static_cast<_Iostate>(0x1);
    static constexpr _Iostate failbit = static_cast<_Iostate>(0x2);
    static constexpr _Iostate badbit  = static_cast<_Iostate>(0x4);
    flag meaning
    goodbit 오류가 없다
    eofbit 스트림으로부터 추출 작업(extracting operation) 진행 중 EOF에 도달
    failbit 마지막 입력 작업이 자체 내부 오류 때문에 실패
    badbit 스트림 버퍼의 입출력 작업이 실패

ios_base

  • operator! 연산자 오버로딩, clear(), rdstate(), setstate(), good(), eof(), fail(), bad() 등의 함수가 내장되어있다.

  • ios::fail()

    • failbit나 badbit가 설정되어있으면 true 반환

    • operator! 는 fail()을 호출한다.

  • ios::clear()

    • 오류 상태 플래그를 새로운 값으로 설정

      • 기본 값은 goodbit : 오류 상태 플래그를 0으로 초기화 시킨다.
  • ios::rdstate()

    • 오류 상태 플래그(error state flag)를 반환한다.

      • 위의 비트들과 AND 연산으로 플래그를 확인할 수 있다.

      • AND 연산은 귀찮으므로 그냥 ios::eof(), ios::fail(), ios::bad(), ios::good() 함수를 사용하자.


basic_ios

  • basic_istream, basic_ostream 클래스의 base class

  • clear(), setstate() 함수의 기본 값 설정


basic_istream

  • 각 자료형에 대해 operator>> 연산자 오버로딩으로 변수에 값을 넣도록 하였다.

    • 처음에 _Err = ios_base::goodbit 로 설정한 뒤 결과에 따라 _Myios::setstate(_Err) 로 오류 상태 플래그를 설정
  • use_facet 관련은 locale을 알아야 할 것 같다. 참고

    template <class _Ty>
      basic_istream& _Common_extract_with_num_get(_Ty& _Val) { // formatted extract with num_get
      ios_base::iostate _Err = ios_base::goodbit;
      const sentry _Ok(*this);
    
      if (_Ok) { // state okay, use facet to extract
        _TRY_IO_BEGIN
          _STD use_facet<_Nget>(this->getloc()).get(*this, {}, *this, _Err, _Val);
        _CATCH_IO_END
      }
    
      _Myios::setstate(_Err);
      return *this;
    }
    
    basic_istream& __CLR_OR_THIS_CALL operator>>(unsigned int& _Val){ // extract an unsigned int
        return _Common_extract_with_num_get(_Val);
    }
    ...

입력 버퍼 무시하기

저작자표시 (새창열림)

'C++ > Library' 카테고리의 다른 글

C++ typeinfo  (0) 2021.03.11
C++ cin.ignore  (0) 2021.03.11
C++ 난수 생성 (Random Number Generation)  (0) 2021.03.11
C++ 입력 버퍼 무시하기  (0) 2021.03.10
C++ typeinfo  (0) 2021.03.10
'C++/Library' 카테고리의 다른 글
  • C++ typeinfo
  • C++ cin.ignore
  • C++ 난수 생성 (Random Number Generation)
  • C++ 입력 버퍼 무시하기
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 분류 전체보기 (317)
      • Algorithm (13)
        • 알기 쉬운 알고리즘 (10)
        • Search (1)
        • Sort (2)
      • Arduino (0)
      • C++ (185)
        • Class (46)
        • Exception (6)
        • Library (51)
        • Overloading (10)
        • SmartPointer (5)
        • Syntax (33)
        • TBC++ (23)
        • Templates (9)
        • VisualStudio (2)
      • Embedded (1)
      • Git (4)
      • Java (5)
      • Linux (16)
        • Error (1)
        • Linux Structure (11)
      • MacOS (7)
      • OS (1)
        • Concurrency (1)
      • Python (21)
        • Class (1)
        • Function (2)
        • Syntax (17)
      • Raspberrypi (9)
      • Review (1)
      • Utility (12)
        • VSCode (5)
        • VirtualBox (3)
      • Web (8)
        • Nginx (1)
        • React (3)
        • Django (1)
      • Windows (20)
        • Registry (3)
        • WSL (1)
        • DeviceDriver (6)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    그림판
    스프링
    제외
    백기선
    mspaint
    KakaoTalk
    windows
    vscode
    SunOS 5.1
    Workspace
    spring
    citrix workspace
    시스템 복구
    윈도우 명령어
    Solaris 10
    logi options
    dism
    EXCLUDE
    맥북 카카오톡 알림 안뜸
    unix
    스프링 프레임워크 핵심 기술
    Windows 11
    윈도우
    SFC
    로지텍 마우스 제스처
    알림
    java
    MacOS
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ cin
상단으로

티스토리툴바