题解 | #参数解析#
参数解析
http://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
#include<iostream>
#include<vector>
using namespace std;
//先检查引号,遇到奇数个引号的时候进入引号内,直到下一个引号的长度解析为一个参数
//再对空格进行判断,如果空格是在引号外,则空格分割参数
vector<string> parameter_analysis(string s){
vector<string> res;
string str="";
bool flag=false;
int n=s.length();
for(int i=0;i<n;i++){
if(s[i]=='\"'){
if(flag==false){
flag=true;
}else{
flag=false;
res.push_back(str);
str.clear();
}
}else{
if(flag==true){
str+=s[i];
}else{
if((s[i]==' ')&&(str.empty()==false)){
res.push_back(str);
str.clear();
}
else if(s[i]!=' '){
str+=s[i];
}
}
}
}
if(str.empty()==false){
res.push_back(str);
}
return res;
}
int main(){
string temp;
while(getline(cin,temp)){
vector<string> str=parameter_analysis(temp);
int m=str.size();
cout<<m<<endl;
for(int i=0;i<m;i++){
cout<<str[i]<<endl;
}
}
return 0;
}