How to parse a comma separated string into a vector with C++

1 Answer

0 votes
#include <iostream>
#include <sstream>
#include <vector>

int main() {
    std::string s = "a,b,4,c++,9,java";
    std::vector<std::string> v;
    
    std::stringstream ss(s);
 
    while (ss.good()) {
        std::string subs;
        getline(ss, subs, ',');
        v.push_back(subs);
    }
 
    for (size_t i = 0; i < v.size(); i++)
        std::cout << v[i] << '\n';
}



/*
run:

a
b
4
c++
9
java

*/

 



answered Feb 14, 2022 by avibootz

Related questions

1 answer 124 views
1 answer 143 views
1 answer 130 views
1 answer 124 views
1 answer 135 views
2 answers 291 views
...