题解 | #参数解析#

参数解析

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

坑还是蛮多的,要注意

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void do_work(vector<string>&result,string str);
int main() {
    string str;
    while(getline(cin,str)){
        vector<string>result;
        do_work(result,str);
        cout<<result.size()<<endl;
        for(string &ch:result){
            cout<<ch<<endl;
        }
    }
    
}
void do_work(vector<string>&result,string str){
    int begin = 0;
    int n = str.size();
    for(int i = 0;i<n;i++){
        if(str[i]==' '&&i>=begin){//如果有一个字符串,结束后begin = i+2,然后i走一步到空格,i = begin-1,此时不加判断的话会加上下一个单词的开头,然后begin = i+1不变,多了一个单字符
            string sub = str.substr(begin,i-begin);
            result.push_back(sub);
            begin = i+1;
        }
        else if(str[i]=='"'){//此时的begin就在前引号的位置
            i++;
            while(str[i]!='"'&&i<str.size()){
                i++;
            }
            //此时i指向后引号,收集字符串
            begin++;
            int len = i-begin;
            string sub = str.substr(begin,len);
            result.push_back(sub);
            begin = i+2;
        }
    }
    //要把最后一个单词收了
    if(begin<n){
        string sub = str.substr(begin);
        result.push_back(sub);
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
05-05 21:45
已编辑
广州大学 Java
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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