题解 | #参数解析#
参数解析
https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
#include <iostream> #include <string> #include <vector> using namespace std; int main() { string a; vector<string> data; while(getline(cin,a)) { int p = 0; bool flag = false; //记录是否进入引号中 for(int i = 0; i < a.length(); i++) { if(a[i] == '"') { //遇到引号 if(!flag) //第一个引号 flag = true; else { //第二个引号 flag = false; data.push_back(a.substr(p, i - p)); //截取字符串加入 } p = i + 1; } else if(a[i] == ' ' && !flag) { //遇到引号外的空格 if(i != p) //非空字符串 data.push_back(a.substr(p, i - p)); //截取字符串加入 p = i + 1; } else if(i == a.length() - 1) //最后一个参数字符串 data.push_back(a.substr(p, i - p + 1)); } } cout << data.size() << endl; for (auto it : data) { cout << it; cout << endl; } } // 64 位输出请用 printf("%lld")