题解 | #参数解析#
参数解析
https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
#include<iostream> #include<string> #include<vector> using namespace std; int main(){ string s; while(getline(cin, s)){ vector<string> output; string temp = ""; bool flag = false; //记录是否进入引号中 for(int i = 0; i < s.length(); i++){ if(flag){ //如果在引号中 if(s[i] != '\"') //遇到非引号都添加为字符串 temp += s[i]; else flag = false; //否则设置出引号 }else{ //如果不在引号中 if(s[i] == ' '){ //遇到空格隔断 output.push_back(temp); temp = ""; }else if(s[i] == '\"') //遇到引号设置为进入引号 flag = true; else //其余添加进字符串 temp += s[i]; } } output.push_back(temp); //最后一段 cout << output.size() << endl; //输出参数个数 for(int i = 0; i < output.size(); i++) cout << output[i] << endl; } return 0; }