题解 | #参数解析#

参数解析

https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    string input;
    vector<string> v;
    vector<string> temp;
    bool on = false;
    while (cin >> input) {
        if (input[0] == '"' && input[input.size() - 1] == '"') {
            input = input.substr(1, input.size() - 2);
            v.push_back(input);
        } else {
            if (on) {
                if (input[input.size() - 1] == '"') {
                    on = false;
                    input = input.substr(0, input.size() - 1);
                    temp.push_back(input);
                    string str;
                    for (auto& i : temp) {
                        str += i;
                        str += " ";
                    }
                    v.push_back(str);
                } else {
                    temp.push_back(input);
                }
            } else {
                if (input[0] == '"') {
                    on = true;
                    input = input.substr(1, input.size() - 1);
                    temp.push_back(input);
                } else {
                    v.push_back(input);
                }
            }
        }
    }
    cout << v.size() << endl;
    for (auto& i : v) {
        cout << i << endl;
    }
}

全部评论

相关推荐

头像
04-29 10:53
已编辑
东北大学 自动化类
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务