Skip to content

Instantly share code, notes, and snippets.

@tatzyr
Created June 21, 2013 13:42
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 tatzyr/5831230 to your computer and use it in GitHub Desktop.
Save tatzyr/5831230 to your computer and use it in GitHub Desktop.
カンマなどで区切られたstringを分割。ダブルクォートや連続したカンマは解釈しない簡易版。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// カンマなどで区切られたstringを分割
vector<string> split(const string line, const string delim)
{
vector<string> result;
size_t len;
string subLine = line;
while ((len = subLine.find_first_of(delim)) != string::npos) {
result.push_back(subLine.substr(0, len));
subLine = subLine.substr(len + 1);
}
// CR(復帰)をついでに除去
len = subLine.find_first_of("\r");
result.push_back(subLine.substr(0, len));
return result;
}
int main()
{
vector<string> result = split("1,2,3,4", ",");
for (int i = 0; i < result.size(); i++) {
cout << result[i] << "-"; // 1-2-3-4-
}
cout << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment