[c++] 문자열을 단어로 분리
#include <iostream>
#include <sstream>
#include <vector>
int main() {
std::string sentence = "문자열을 단어로 분리하는 예시입니다";
std::istringstream iss(sentence);
std::vector<std::string> words{std::istream_iterator<std::string>{iss}, std::istream_iterator<std::string>{}};
for (const auto& word : words) {
std::cout << word << std::endl;
}
return 0;
}
이 코드는 std::istringstream
를 사용하여 문자열을 단어로 분리합니다. 문자열을 iss
에 넣은 후 std::vector<std::string>
을 사용하여 단어로 분리한 뒤 출력합니다.
다음과 같은 결과가 출력됩니다:
문자열을
단어로
분리하는
예시입니다
이 코드는 표준 라이브러리를 사용하여 간단한 방법으로 문자열을 단어로 분리하는 예제를 보여줍니다. 이 방법을 사용하면 문자열을 효과적으로 분석할 수 있습니다.