Simplest way to split string in c++, Split string using stream/iterator, 스페이스 기준 문자열/단어 나누기, c++, java, stl, std
스페이스를 기준으로 string 을 각 단어별로 나눈다.
첫번째는, 몇단어나 포함된 문자열인지 모를 때, iterator 를 사용하여 시작부터 끝까지 전체를 잘라서 vector 에 넣어준다.
두번째는, 몇단어가 포함된 문자열인지 알 때, for 문에서 반복 횟수를 정해두고 가져온다. 조금 더 보기 간결하다.
Split each words on every white space.
First solution, if we don't know how many words are in a string text, use iterator with stream and put result words in vector.
Second solution, if we know how many words are in a string text, use for statement and get each words.
Code.. Lemme see code...
코드.. 코드를 보자
e.g.
#include <iostream>
#include <iterator> //
int main(int argc, char *argv[]) {
// 1st
std::string string1 = "Let us split this text into each words";
std::istringstream inputStream1(string1);
std::vector<std::string> results((std::istream_iterator<std::string>(inputStream1)),
std::istream_iterator<std::string>());
for (int i = 0; i < results.size(); ++i)
{
cout << results[i] << endl;
}
///////////////////////////////////////////////////////
cout << endl;
// 2nd
std::string string2 = "125 320 512 750 333";
std::istringstream inputStream2(string2);
int val;
for (int i = 0; i < 5; ++i)
{
inputStream2 >> val;
cout << val << endl;
}
return 0;
}
Something else you might like...?
2019/01/25 - [Life/Health care] - Maca 마카 usage/side effects/dosage 효능/부작용/성인,소아 용법(2)
2019/01/25 - [Life/Health care] - L-Arginine 아르기닌 usage/side effects/dosage 효능/부작용/성인,소아 용법(3)