string split

2018. 11. 21. 18:37Programming/c++

  1. std::vector<std::stringsplit(const std::string& s, char delimiter)
  2. {
  3.     std::vector<std::string> tokens;
  4.     std::string token;
  5.     std::istringstream tokenStream(s);
  6.     while (std::getline(tokenStream, token, delimiter))
  7.     {
  8.         token.erase(std::remove(token.begin(), token.end()' '), token.end());
  9.         tokens.push_back(token);
  10.     }
  11.     return tokens;
  12. }


  1. int main()
  2. {
  3.     std::string missionRewardCnt = "5 | 10 | 18";
  4.     std::vector<std::string> result = split(missionRewardCnt, '|');
  5.  
  6.     for (auto it = result.begin(); it != result.end(); it++)
  7.     {
  8.         std::cout << *it << std::endl;
  9.     }
  10. }

String 데이터 "5 | 10 | 18"이 있다고 가정 했을 때,

String을 '|'으로 split 하고 양 옆에 공백을 제거 하는 함수


std::remove는 #inlcude <algorithm>을 해줘야 함

std::istringstream은 #include <sstream>을 해줘야 함

'Programming > c++' 카테고리의 다른 글

[c++] jsoncpp 빌드 및 사용방법  (1) 2019.05.27
union을 사용해서 4byte 인덱스 조합하기  (0) 2018.12.04
rvalue 참조 사이트  (0) 2017.02.17