定义一个bool变量 判断是否在引号里面
#include <iostream>
#include <vector>
using namespace std;
int main() {
string command, tmp;
vector<string> ans;
bool in_quote = false;
getline(cin, command);
for(int i = 0; i < command.size(); ++i) {
if(command[i] == '"') {
in_quote = !in_quote;
if(!in_quote) { //如果没有在引号里面 即此时是在引号的最右侧 直接push就行
ans.push_back(tmp);
tmp = "";
}
} else if(command[i] == ' ' && !in_quote) {
if(!tmp.empty()) {
ans.push_back(tmp);
tmp = "";
}
} else tmp += command[i];
}
if(!tmp.empty()) {
ans.push_back(tmp);
}
cout << ans.size() << endl;
for(const string& s: ans) {
cout << s << endl;
}
return 0;
}



京公网安备 11010502036488号