想知道为什么getline一个样例都过不了,改用cin输入却可以
查看原帖
想知道为什么getline一个样例都过不了,改用cin输入却可以
796426
wubuchifeirou楼主2024/11/30 00:50
#include<iostream>
#include<set>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;
int n;
string s;
vector<string> file;

vector<string> substr(string st) {
	vector<string> t;
	string l = "";
	int len = (int)(st.length());
	for (int i = 0; i < len; i++) {
		if (st[i] == ' ') {
			t.push_back(l);
			l = "";
		}
		else {
			l += st[i];
		}
	}
	t.push_back(l);
	return t;
}

int main()
{
	cin >> n;
	getchar();
	for (int i = 0; i < n; i++) {
		getline(cin, s);
		vector<string> t = substr(s);
		if (t[0] == "touch") {
			auto id = find(file.begin(), file.end(), t[1]);
			if (id == file.end())
				file.push_back(t[1]);
		}
		else if (t[0] == "rename") {
			auto id1 = find(file.begin(), file.end(), t[1]);
			auto id2 = find(file.begin(), file.end(), t[2]);
			if (id2 == file.end() && id1 != file.end()) {
				*(id1) = t[2];
			}
		}
		else if (t[0] == "rm") {
			auto id = find(file.begin(), file.end(), t[1]);
			if (id != file.end())
				file.erase(id);
		}
		else if (t[0] == "ls") {
			for (auto i : file) {
				cout << (i) << endl;
			}
		}
	}
	return 0;
}
2024/11/30 00:50
加载中...