Last active
December 15, 2015 03:29
-
-
Save raimusyndrome/3b71383d167cd861b3cc to your computer and use it in GitHub Desktop.
Boost.Tokenizerで文字列を分割。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <boost/tokenizer.hpp> | |
int main() { | |
// トークンのセパレータ | |
typedef boost::char_separator<char> char_sep; | |
// トークン処理器 | |
typedef boost::tokenizer<char_sep> char_tok; | |
// 分割対象文字列 | |
std::string str = "vim,vimmer,vimmest"; | |
// デリミタ(カンマ区切り) | |
char_sep comma(","); | |
char_tok tok(str, comma); | |
std::string substr[3]; | |
int i = 0; | |
std::cout << str << std::endl; | |
// 分割後の文字列を取得 | |
for( char_tok::iterator it = tok.begin(); it != tok.end(); it++ ){ | |
substr[i] = *it; | |
std::cout << i << "=" << substr[i] << std::endl; | |
i += 1; | |
} | |
// 空白のみの部分も分割する | |
std::string sp_str = "1:50: : 24: 7"; | |
// デリミタ(コロン区切り) | |
// 空白のみのトークンも保持する | |
char_sep colon(":", NULL, boost::keep_empty_tokens); | |
// 再設定 | |
tok.assign(sp_str, colon); | |
std::string sp_tokens[5]; | |
i = 0; | |
std::cout << sp_str << std::endl; | |
// 分割後の文字列を取得 | |
for( char_tok::iterator it = tok.begin(); it != tok.end(); it++ ){ | |
sp_tokens[i] = *it; | |
std::cout << i << "=" << sp_tokens[i] << std::endl; | |
i += 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment