Home >Backend Development >C++ >How to Parse a Comma-Separated String into an Integer Array in C ?
Parsing Comma-separated Strings into Integer Arrays
In C , parsing a comma-separated string into an integer array can be achieved using the following steps:
Here's an example code snippet that demonstrates this approach:
#include <vector> #include <string> #include <sstream> #include <iostream> int main() { std::string str = "1,2,3,4,5,6"; std::vector<int> vect; std::stringstream ss(str); for (int i; ss >> i;) { vect.push_back(i); if (ss.peek() == ',') ss.ignore(); } for (std::size_t i = 0; i < vect.size(); i++) std::cout << vect[i] << std::endl; }
Output:
1 2 3 4 5 6
The above is the detailed content of How to Parse a Comma-Separated String into an Integer Array in C ?. For more information, please follow other related articles on the PHP Chinese website!