C++ - Storing user input string separated by commas into vector

前端 未结 3 1369
野趣味
野趣味 2021-01-07 09:23

I have a code written that performs this task to a certain extent. But, I would like to how to alter my code so that I can store as many string inputs the user wants to ente

3条回答
  •  春和景丽
    2021-01-07 10:01

    There are much simpler approaches to parse an input string using stringstreams:

    string a;
    vector objects;
    
    for(stringstream sst(input); getline(sst, a, ','); )  // that's all ! 
        objects.push_back(a);
    
    copy (objects.begin(), objects.end(), ostream_iterator(cout," ; "));  // display all
    

    Online demo

提交回复
热议问题