| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 |
Tags
- 게임 서버 아키텍처
- Spin Lock
- select 모델
- Strongly Connected Component
- Lock-free Stack
- Prisma
- map
- Behavior Design Pattern
- reference counting
- Overlapped Model
- JavaScript
- 비트필드를 이용한 dp
- 비트마스킹
- 강한 연결 요소
- 그래프 탐색
- 벨만-포드
- 최소 공통 조상
- Delete
- PROJECT
- 트라이
- 2-SAT
- SCC
- HTTP
- Github
- ccw 알고리즘
- 자바스크립트
- Binary Lifting
- DP
- trie
- 이분 탐색
Archives
- Today
- Total
dh_0e
[C++] stringstream 본문
stringstream
- 문자열에서 동작하는 스트림 클래스
- 입력 스트림인 istringstream, 출력 스트림인 ostringstream의 기능을 둘 다 포함하고 있음
- 공백이나 '\n' 개행문자를 기준으로 값을 분리해줌
- string type의 문자열에서 원하는 자료형의 데이터를 추출할 수 있게 해줌
- <sstream> 헤더 파일을 선언하고 사용
문자열을 공백, '\n'을 기준으로 분리
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string n;
string str = "abc d ef\n gh i1 2 3";
stringstream stream;
stream.str(str); // = "stream=str"
while(stream >> n)
{
cout << n << endl; // abc d ef gh i1 2 3
}
return 0;
}
문자열에서 int type 추출
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int n;
string str = "111 -222\n 11+100 ++1000";
stringstream stream;
stream.str(str); // = "stream=str"
while(stream >> n)
{
cout << n << endl; // 111 -222 11 100
}
return 0;
}
- -222와 +100도 인식하지만 ++1000은 +를 문자로 인식하여 추출하지 않는다.
'C++' 카테고리의 다른 글
| [C++] lambda, 타입 추론(auto), 일반화 함수(template), Singleton in C++ (3) | 2025.08.05 |
|---|---|
| [C++] memset, isupper/islower (0) | 2025.02.13 |
| [C++] max_element, min_element, find (0) | 2024.07.12 |
| [C++] map, unordered_map (0) | 2024.07.03 |
| [C++] 행렬 곱셈(matrix multiplication) 정의와 그 이유, 구현 (0) | 2024.06.25 |