题解 | #参数解析#

参数解析

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

//分享思路,其实就是判断几种情况
//如果是引号就循环读完,并且需要跳过引号后的空格
//如果是空格并且tmp里面有东西,就直接加到结果里去
//不符合上述两种情况就往临时string里加字符
#include<iostream>
#include<vector>
#include<string>
using namespace std;

int main(){
    string inputstr;
    getline(cin, inputstr);
    inputstr += " ";//保证最后一个字符串的输出,因为会去判断空格
    vector<string> strres;
    string tmp;
    for(int i = 0;i<inputstr.size();++i){
        if(inputstr[i] == '"'){
            ++i;
            while(inputstr[i]!='"'){
                tmp+=inputstr[i++];
            }
            ++i;
            strres.emplace_back(tmp);
            tmp = "";
            continue;
        }
        if(inputstr[i] == ' ' && tmp!=""){
            strres.emplace_back(tmp);
            tmp = "";
            continue;
        }
        tmp+=inputstr[i];
    }
    //strres.emplace_back(tmp);
    cout << strres.size()<< endl;
    for(int i = 0;i<strres.size();++i){
        cout << strres[i]<< endl;
    }
    return 0;
}

全部评论

相关推荐

不要停下啊:大二打开牛客,你有机会开卷了,卷起来,去找课程学习,在牛客上看看大家面试笔试都需要会什么,岗位有什么需求就去学什么,努力的人就一定会有收获,这句话从来都经得起考验,像我现在大三了啥也不会,被迫强行考研,炼狱难度开局,啥也不会,找工作没希望了,考研有丝丝机会
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务