Created
June 21, 2013 13:42
-
-
Save tatzyr/5831230 to your computer and use it in GitHub Desktop.
カンマなどで区切られたstringを分割。ダブルクォートや連続したカンマは解釈しない簡易版。
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 <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