stringstream与getline()分割字符串

 getline(cin, line);
    line = line.substr(1, line.size() - 2);
    stringstream ss;
    ss.str(line);
    string item;
    char delim = ',';
    while (getline(ss, item, delim)) {
        output.push_back(stoi(item));
    }
 
方法二:
        list<string> dataArray;
        string str;
        for (auto& ch : data) {
            if (ch == ',') {
                dataArray.push_back(str);
                str.clear();
            } else {
                str.push_back(ch);
            }
        }
        if (!str.empty()) {
            dataArray.push_back(str);
            str.clear();
        }
posted @ 2022-02-23 10:11  FredMoMo  阅读(149)  评论(0)    收藏  举报