Skip to content

Instantly share code, notes, and snippets.

@meki
Created October 9, 2015 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meki/4226bfb3abf729c585cb to your computer and use it in GitHub Desktop.
Save meki/4226bfb3abf729c585cb to your computer and use it in GitHub Desktop.
文字列を指定した区切り文字列で分割する関数 ref: http://qiita.com/_meki/items/4328c98964ea33b0db0d
#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 << " / ";
}
}
私の / 名前は / 太郎 / です / /
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment