题解 | #参数解析#
参数解析
http://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
#include <bits/stdc++.h>
using namespace::std;
int main() {
string str;
while (getline(cin, str)) {
vector<string> vec;
int j = 0; //记录起始索引
for (int i = 0; i < str.size(); i++) {
if (' ' == str[i]) {
if (i != j) { //避免空串
vec.push_back(str.substr(j, i - j));
}
j = i + 1;
} else if ('"' == str[i]) {
j = i + 1;
i++;
while (str[i] != '"') {
i++;
}
if (i != j) {
vec.push_back(str.substr(j, i - j));
}
j = i + 1;
}
}
if (j < str.size()) { // 最后如果还剩一串字符
vec.push_back(str.substr(j, str.size() - j));
}
cout << vec.size() << endl;
for (auto const &v : vec) {
cout << v << endl;
}
}
return 0;
}