题解 | #参数解析#
参数解析
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")