Skip to content

Instantly share code, notes, and snippets.

@raimusyndrome
Last active December 15, 2015 03:29
Show Gist options
  • Save raimusyndrome/3b71383d167cd861b3cc to your computer and use it in GitHub Desktop.
Save raimusyndrome/3b71383d167cd861b3cc to your computer and use it in GitHub Desktop.
Boost.Tokenizerで文字列を分割。
#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