일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 자바스크립트
- 이분 탐색
- MySQL
- Prisma
- map
- string
- HTTP
- 그리디
- 백준 9527번
- Next
- 그래프 탐색
- vector insert
- 게임 서버 아키텍처
- localstorage
- JavaScript
- 트라이
- branch
- PROJECT
- insomnia
- pm2
- Express.js
- trie
- ERD
- Keys
- DP
- router
- html5
- ccw 알고리즘
- Github
- MongoDB
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++] 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 |
[C++] stoi/stol/stoll, <string> method (0) | 2024.06.24 |