C++

[C++] <sstream>헤더파일

programmer-faust 2025. 7. 4. 22:59
  • 순서
    1. sstream이란 무엇인가?
    2. sstream이 제공하는 주요 클래스
  • sstream이란 무엇인가?
    1. C++ 표준 라이브러리의 헤더파일이다.
    2. string 기반 stream class들을 제공하여 문자열(string)을 입력/출력 stream처럼 처리할 수 있도록 도와줌
  • sstream이 제공하는 주요 클래스
    1. istringstream
      • 문자열로부터 입력 스트림을 생성함.
      • istringstream ex)
        #include <sstream>
        #include <iostream>
        #include <string>
        using namespace std;
        
        int main() {
            string data = "123 456 789";
            istringstream iss(data);
        
            int a, b, c;
            iss >> a >> b >> c;
        
            cout << a << ", " << b << ", " << c << endl;  // 출력: 123, 456, 789
        }
    2. ostringstream
      • 출력 스트림에 데이터를 쓰면 문자열로 저장됨.
      • ostringstream ex)
        #include <sstream>
        #include <iostream>
        #include <string>
        using namespace std;
        
        int main() {
            ostringstream oss;
            oss << 123 << " " << 456 << " " << 789;
        
            string result = oss.str();
            cout << result << endl;  // 출력: 123 456 789
        }
    3. stringstream
      • 입력과 출력이 모두 가능함.
      • stringstream ex)
        #include <sstream>
        #include <iostream>
        #include <string>
        using namespace std;
        
        int main() {
            stringstream ss;
            ss << "100 200 300";
        
            int x, y, z;
            ss >> x >> y >> z;
        
            cout << x << ", " << y << ", " << z << endl;  // 출력: 100, 200, 300
        }

 

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

[C++] Iterator 반복자  (0) 2025.07.10
[C++]Alogorithm헤더파일  (1) 2025.07.08
[C++]객체 지향 프로그래밍 OOP 4가지 특징  (0) 2025.07.03
[C++] toupper, tolower  (0) 2025.07.02
[C++]TRPG(TextRPG) Inventory제작  (0) 2025.06.23