看了csdn上的一些答案,感觉上来就
list List[10000]
这也太废内存了,啥家庭啊!敢这么用。
所以就做了个稍微靠谱点的版本,见笑:
#include <iostream>
#include <list>
#include <map>
#include <sstream>
#include <string>
#include <utility>
using namespace std;
#define ADD "add"
#define NEW "new"
#define MERGE "merge"
#define OUT "out"
#define UNIQUE "unique"
class List {
public:
map<int, list<int> *> g_map;
list<int> name_list;
List(){};
~List() {
for (int i = 0; i < name_list.size(); i++) {
delete g_map[name_list.front()];
name_list.pop_front();
}
};
void when_new(int key) {
g_map.insert(pair<int, list<int> *>(key, new (list<int>)));
name_list.push_back(key);
}
void add(int id, int num) { g_map[id]->push_back(num); }
void merge(int id_1, int id_2) {
g_map[id_1]->merge(*g_map[id_2]);
g_map[id_1]->sort();
}
void out(int key) {
list<int>::iterator tmp = g_map[key]->begin();
for (int i = 0; i < g_map[key]->size(); i++, tmp++) {
cout << (int)*tmp << " ";
}
cout << endl;
}
void unique(int key) { g_map[key]->unique(); }
};
int main() {
List list_tmp;
int n_line_cmd, first, second;
cin >> n_line_cmd;
string head;
while (cin >> head) {
if (head.find(ADD) <= head.size()) {
cin >> first >> second;
list_tmp.add(first, second);
} else if (head.find(NEW) <= head.size()) {
cin >> first;
list_tmp.when_new(first);
} else if (head.find(MERGE) <= head.size()) {
cin >> first >> second;
list_tmp.merge(first, second);
} else if (head.find(OUT) <= head.size()) {
cin >> first;
list_tmp.out(first);
} else if (head.find(UNIQUE) <= head.size()) {
cin >> first;
list_tmp.unique(first);
}
head.clear();
}
}
欢迎讨论