Created
October 9, 2015 16:43
-
-
Save meki/4226bfb3abf729c585cb to your computer and use it in GitHub Desktop.
文字列を指定した区切り文字列で分割する関数 ref: http://qiita.com/_meki/items/4328c98964ea33b0db0d
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 <string> | |
template <typename List> | |
void split(const std::string& s, const std::string& delim, List& result) | |
{ | |
result.clear(); | |
using string = std::string; | |
string::size_type pos = 0; | |
while(pos != string::npos ) | |
{ | |
string::size_type p = s.find(delim, pos); | |
if(p == string::npos) | |
{ | |
result.push_back(s.substr(pos)); | |
break; | |
} | |
else { | |
result.push_back(s.substr(pos, p - pos)); | |
} | |
pos = p + delim.size(); | |
} | |
} | |
// ====== SAMPLE ====== | |
#include <vector> | |
#include <iostream> | |
using namespace std; | |
int main(void) | |
{ | |
std::vector<string> result; | |
split("私の@@名前は@@太郎@@です@@", "@@", result); | |
for(auto s : result) { | |
cout << s << " / "; | |
} | |
} |
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
私の / 名前は / 太郎 / です / / |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment