C++ sstream

2021. 3. 26. 15:28·C++/Library

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
  */
저작자표시 (새창열림)

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

C++ istream  (0) 2021.03.29
C++ wstring  (0) 2021.03.26
C++ string  (0) 2021.03.26
C++ STL 알고리즘 (Algorithms)  (0) 2021.03.26
C++ STL 반복자 (Iterators)  (0) 2021.03.26
'C++/Library' 카테고리의 다른 글
  • C++ istream
  • C++ wstring
  • C++ string
  • C++ STL 알고리즘 (Algorithms)
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)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

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

티스토리툴바