C++
[C++] 문자열(string)을 특정 문자로 자르기
G A
2021. 4. 24. 03:31
문자열을 sstream을 사용하여 특정 문자열로 구분하여 자르기
아래 코드는 공백 문자를 기준으로 자르는 방법
#include <string>
#include <vector>
#include <sstream>
using namespace std;
int main() {
string fruit = "apple lemon grape melon";
istringstream ss(fruit);
vector <string> words;
string word;
while (getline(ss, word, ' ')) //공백 단위로 구분
words.push_back(word);
//words에는 apple, lemon, grape, melon이 저장됨
}
반응형