c++ split string by comma
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
int main() {
std::string inputString = "apple,orange,banana,grape";
std::vector<std::string> tokens;
std::istringstream tokenStream(inputString);
std::string token;
while (std::getline(tokenStream, token, ',')) {
tokens.push_back(token);
}
// Output the result
for (const auto &t : tokens) {
std::cout << t << std::endl;
}
return 0;
}