Skip to content

Instantly share code, notes, and snippets.

@taisyo7333
Created November 8, 2015 12:35
Show Gist options
  • Save taisyo7333/b4a5cadf05deb00e7eff to your computer and use it in GitHub Desktop.
Save taisyo7333/b4a5cadf05deb00e7eff to your computer and use it in GitHub Desktop.
半角空白で区切られた単語を単語ごとに分割して配列に格納する方法。(再帰版)
This
is
a
pen
#include<vector>
#include<string>
using namespace std;
void split(std::vector<string>& v, string s,const string::value_type code)
{
auto pos = s.find(code);
if (pos == string::npos) {
// last word
if (!s.empty()) {
v.push_back(s);
}
return;
}
string ns(s.begin(), s.begin() + pos);
v.push_back(ns);
if (s.begin() + pos + 1 == s.end())
return;
string next(s.begin() + pos + 1, s.end());
split(v, next, code);
}
#include<iostream>
int _tmain(int argc, _TCHAR* argv[])
{
vector<string> v;
string statement = "This is a pen";
split(v, statement, ' ');
for (auto s : v) {
std::cout << s << std::endl;
}
return 0;
}
@taisyo7333
Copy link
Author

英文を抽出する場合は、
英単語、数字、',' (comma)、'.' (period) をそれぞれ識別する必要がある。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment