Boost.String Algorithmにあるsplit便利ですね。
今まで自分で作ってました。
#include <iostream> #include <vector> #include <string> #include <boost/algorithm/string.hpp> #include <boost/foreach.hpp> #define foreach BOOST_FOREACH using namespace std; using namespace boost::algorithm; int main() { string s = "a b c"; vector<string> result; split(result, s, boost::is_space()); // スペースで区切る foreach (const string& item, result) { cout << item << endl; } }
a b c
splitに渡す述語をis_any_ofにすれば任意の文字列でデリミタで区切れます
#include <iostream> #include <vector> #include <string> #include <boost/algorithm/string.hpp> #include <boost/foreach.hpp> #define foreach BOOST_FOREACH using namespace std; using namespace boost::algorithm; int main() { string s = "a\nb\nc"; vector<string> result; split(result, s, boost::is_any_of("\n")); // 改行で区切る foreach (const std::string& item, result) { cout << item << endl; } }
a b c