php 의 explode 와 같이 문자열을 특정 구분자로 배열로 변환해 보겠습니다.
#include <iostream> #include <stdio.h> #include <cstdlib> #include <iostream> #include <stdexcept> #include <string> #include <vector> #include <sstream> #include <utility> std::string exec(const char* cmd) { char buffer[128]; std::string result = ""; FILE* pipe = popen(cmd, "r"); if (!pipe) throw std::runtime_error("popen() failed!"); try { while (fgets(buffer, sizeof buffer, pipe) != NULL) { result += buffer; } } catch (...) { pclose(pipe); throw; } pclose(pipe); return result; } template <typename Out> void split(const std::string &s, char delim, Out result) { std::istringstream iss(s); std::string item; while (std::getline(iss, item, delim)) { *result++ = item; } } std::vector<std::string> split(const std::string &s, char delim) { std::vector<std::string> elems; split(s, delim, std::back_inserter(elems)); return elems; } int main() { std::vector<std::string> x = split("one:two::three", ':'); for(int i=0; i<x.size(); i++) { std::cout << x[i] << std::endl; } }
result
$ ./cpp one two three
'프로그래밍 > C++' 카테고리의 다른 글
[NH] centos 7 python3 easyOCR 사용하기 위한 기본 모듈 설치 (0) | 2021.12.10 |
---|---|
c++ system 함수 output 변수에 담아 전달 방법 (0) | 2021.12.08 |
c++ system 함수를 이용한 외부 파일 실행 (0) | 2021.12.07 |
centos7 c++ 에서 http_client.h install 하는 방법 (rest sdk build) (0) | 2021.11.02 |
centos7 c++ Hello World 만들기 (0) | 2021.11.02 |