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/02/12 - [Programming/C++] - How to use map in c++ STL, c++ 맵 STL 사용방법/초기화/예제, java, example, sample, library, find, insert, first
2019/02/04 - [Programming/C++] - How to add DLL / external lib file to VisualStudio, 비주얼스튜디오 외부라이브러리 dll 파일 추가 방법, example, c++ java api
2019/01/30 - [Programming/Software Architecture] - What is Object Oriented Design/Artchitecture? 객체지향 디자인/설계란? java,c++,softwareArchitecture,designPattern
2019/01/29 - [Programming/Programming Language] - 클린코딩/더 나은 코딩을 하는 10가지 방법, 10 Tips for clean code/ better code/ quality code.
2018/12/28 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 - (#9, Tell, Don't Ask)
2018/12/26 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 (1)
2018/11/28 - [Programming/Image Processing] - OpenCV How to handle single channel image matrix each pixel, add/subtract/multiply/division (1), 이미지 매트릭스 각 픽셀단위 값 조정하기 (영상처리, image processing) (1)
2018/11/27 - [Programming/Image Processing] - OpenCV Java complexNumber mat conjugation, 자바 켤레복소수 example code 예제코드
2019/02/14 - [Algorithm/Leet Code] - LeetCode #283 MoveZeroes. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접
2019/02/13 - [Algorithm/Leet Code] - LeetCode #258 AddDigits. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접
2019/02/12 - [Algorithm/Leet Code] - LeetCode #136 SingleNumber. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접
2019/02/10 - [Algorithm/Leet Code] - LeetCode #171 ExcelSheetColumnNumber. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접
2019/02/03 - [Algorithm/Leet Code] - LeetCode #393 UTF-8 Validation. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접
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)
2019/01/08 - [Life/Health care] - VitaminD 비타민디 usage/side effects/dosage/fatigue/supplement/antioxidant/효능/부작용/성인,소아 용법/건강/피로회복/영양제/항산화
2019/01/17 - [Life/Health care] - Zinc 아연 usage/side effects/dosage/fatigue/supplement/antioxidant/효능/부작용/성인,소아 용법/건강/피로회복/영양제/항산화
2019/01/02 - [Life/Health care] - Taurine 타우린 usage/side effects/dosage/fatigue/supplement,효능/부작용/성인,소아 용법/건강/피로회복/영양제
2018/12/27 - [Life/Health care] - Milk-Thistle 밀크시슬 usage/side effects/dosage/fatigue/supplement,효능/부작용/성인,소아 용법/건강/피로회복/영양제