Created
January 5, 2015 13:22
-
-
Save denkiwakame/c665e50c85eb4edbd055 to your computer and use it in GitHub Desktop.
getline for split
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 <string> | |
#include <sstream> | |
#include <vector> | |
std::vector<std::string> split(const std::string& input, char delimiter) | |
{ | |
std::istringstream stream(input); | |
std::string field; | |
std::vector<std::string> result; | |
while (std::getline(stream, field, delimiter)) { | |
result.push_back(field); | |
} | |
return result; | |
} | |
int main() | |
{ | |
const std::string input = "aaa,bbb,ccc"; | |
for (const std::string& s : split(input, ',')) { | |
std::cout << s << std::endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment