C++ sstream

2021. 3. 26. 15:28·SW개발/C++
반응형

sstream

  • <sstream> 라이브러리

  • stringstream의 약자이다.

  • << : insertion operator

  • >> : extraction operator

상속 관계

  • istringstream, ostringstream, stringstream은 비슷하지만 약간 다른 부분이 존재한다.

    • istringstream에 << 연산자는 정의되어있지 않다.

    • ostringstream에 >> 연산자는 정의되어있지 않다.

    <iosfwd>

    using istringstream = basic_istringstream<char, char_traits<char>, allocator<char>>;
    using ostringstream = basic_ostringstream<char, char_traits<char>, allocator<char>>;
    using stringstream  = basic_stringstream<char, char_traits<char>, allocator<char>>;

    <sstream>

    // CLASS TEMPLATE basic_istringstream
    template <class _Elem, class _Traits, class _Alloc>
    class basic_istringstream : public basic_istream<_Elem, _Traits> { // input stream associated with a character array
    };
    
    // CLASS TEMPLATE basic_ostringstream
    template <class _Elem, class _Traits, class _Alloc>
    class basic_ostringstream : public basic_ostream<_Elem, _Traits> { // output stream associated with a character array
    }
    
    // CLASS TEMPLATE basic_stringstream
    template <class _Elem, class _Traits, class _Alloc>
    class basic_stringstream
        : public basic_iostream<_Elem, _Traits> { // input/output stream associated with a character array
    }

기본 예제

  • stringstream 사용

    #include <iostream>
    #include <sstream>
    
    int         main()
    {
        using namespace std;
    
        stringstream ss;
        ss << "Hello, World";
        ss << '!';
    
        string str;
        ss >> str;
        cout << str << endl;
    
        ss.str("Hello World!");
        str = ss.str();
        cout << str << endl;
    
        ss.str("");
        cout << boolalpha << ss.str().empty() << endl;
    }
    
    /* stdout stderr
    Hello,
    Hello World!
    true
    */
  • 숫자 넣기

    #include <iostream>
    #include <sstream>
    
    int         main()
    {
        using namespace std;
    
        stringstream ss;
    
        ss << "12345 67.89";
    
        int     i;
        double  d;
    
        ss >> i >> d;
    
        cout << i << '|' << d << endl;
    }
    
    

/* stdout stderr
12345|67.89
*/


- `istringstream`, `ostringstream` 사용

  ```c++
  #include <iostream>
  #include <string>
  #include <sstream>

  template <typename T>
  std::string ToString(T x)
  {
    std::ostringstream osstream;
    osstream << x;
    return osstream.str();
  }

  template <typename T>
  bool        FromString(const std::string& str, T& x)
  {
    std::istringstream isstream(str);
    return (isstream >> x ? true : false);
  }

  int         main()
  {
    using namespace std;

    string my_str(ToString(3.141592));
    cout << my_str << endl;

    double d;
    if (FromString(my_str, d))
      cout << d << endl;
    else
      cout << "Cannot conver string to double\n";
    if (FromString("Hello", d))
      cout << d << endl;
    else
      cout << "Cannot conver string to double\n";
  }

  /* stdout stderr
  3.14159
  3.14159
  Cannot conver string to double
  */
반응형
저작자표시 (새창열림)

'SW개발 > C++' 카테고리의 다른 글

따라하며 배우는 C++ 18장  (0) 2021.03.26
C++ wstring  (0) 2021.03.26
C++ string  (0) 2021.03.26
따라하며 배우는 C++ 17장  (0) 2021.03.26
C++ STL 알고리즘 (Algorithms)  (0) 2021.03.26
'SW개발/C++' 카테고리의 다른 글
  • 따라하며 배우는 C++ 18장
  • C++ wstring
  • C++ string
  • 따라하며 배우는 C++ 17장
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 전체보기 (319)
      • SW개발 (268)
        • Java Spring (6)
        • C++ (186)
        • Python (21)
        • Linux (16)
        • 알고리즘 (13)
        • Git (4)
        • Embedded (1)
        • Raspberrypi (9)
        • React (3)
        • Web (2)
        • Windows Device Driver (6)
      • IT(개발아님) (46)
        • Windows (26)
        • MacOS (7)
        • Utility (11)
      • 챗봇 짬통 (0)
      • 일상 (2)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

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

티스토리툴바