Created
November 8, 2015 12:35
-
-
Save taisyo7333/b4a5cadf05deb00e7eff to your computer and use it in GitHub Desktop.
半角空白で区切られた単語を単語ごとに分割して配列に格納する方法。(再帰版)
This file contains hidden or 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
This | |
is | |
a | |
pen |
This file contains hidden or 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<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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
英文を抽出する場合は、
英単語、数字、',' (comma)、'.' (period) をそれぞれ識別する必要がある。