0

我试图用逗号分隔这个字符串作为分隔符。我输入了一个字符串“Smith,Erdos,William”,它只输出“William”,而不是 Smith 和 Erdos。这里一定有什么问题,我只是看不到,有人可以帮忙吗?

#include <iostream>
#include <string>
#include <vector>
#include <sstream>

int main() {
    int numScenarios(0);
    int numPapers(0);
    int numWriters(0);
    std::vector<std::string> paperTitles (1);
    std::vector<std::string> paperAuthors (1);
    std::vector<std::string> splitAuthors (1);
    std::string token;
    std::string input;
    std::cin >> numScenarios;
    std::cin >> numPapers >> numWriters; 
    for (int i(0); i < numPapers; ++i) {
        std::getline(std::cin,input);
        std::istringstream iss(input);
        while(getline(iss,token,','));
        {
            std::cout << token << std::endl;
        }
        //paperTitles.push_back(input);
        //input = '\0';
    }
    for (int i(0); i < numWriters; ++i) {
        getline(std::cin,input);
        paperAuthors.push_back(input);
        input = '\0';
    }


return 0;
}
4

1 回答 1

9

while(getline(iss,token,',')); // <== look closely

于 2012-09-05T20:19:59.240 回答