Write a program to complete the following command: new id - create a specified sequence of Numbers for id (id < 200000) Add id num - to join the sequence of Numbers for id integer num merge id1 id2 - merger sequence number of id1 and id2, and make id2 empty unique id - remove repetitive element in the sequence id out id - from small to large, output the elements of sequence of Numbers for ids separated by Spaces给出c++实现代码
时间: 2024-03-22 07:41:54 浏览: 171
MIL-STD-188-114A.021120.PDF
以下是一个简单的 C++ 实现代码,实现了上述指令要求的功能:
```c++
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_set>
using namespace std;
unordered_set<int> idSet; // 存储已经使用的 id
vector<vector<int>> seqs; // 存储每个 id 对应的序列
// 创建一个指定 id 的序列
void createSeq(int id) {
if (idSet.find(id) != idSet.end()) {
cout << "id " << id << " already exists!" << endl;
return;
}
if (id >= 200000) {
cout << "id " << id << " is too large!" << endl;
return;
}
idSet.insert(id);
seqs.emplace_back();
cout << "create sequence for id " << id << " successfully!" << endl;
}
// 向指定 id 的序列中添加一个元素
void addNum(int id, int num) {
auto it = find(idSet.begin(), idSet.end(), id);
if (it == idSet.end()) {
cout << "id " << id << " does not exist!" << endl;
return;
}
seqs[it - idSet.begin()].push_back(num);
cout << "add " << num << " to sequence for id " << id << " successfully!" << endl;
}
// 合并两个 id 对应的序列,将结果存储到第二个 id 对应的序列中
void mergeSeq(int id1, int id2) {
auto it1 = find(idSet.begin(), idSet.end(), id1);
if (it1 == idSet.end()) {
cout << "id " << id1 << " does not exist!" << endl;
return;
}
auto it2 = find(idSet.begin(), idSet.end(), id2);
if (it2 == idSet.end()) {
cout << "id " << id2 << " does not exist!" << endl;
return;
}
auto& seq1 = seqs[it1 - idSet.begin()];
auto& seq2 = seqs[it2 - idSet.begin()];
seq2.insert(seq2.end(), seq1.begin(), seq1.end());
seq1.clear();
cout << "merge sequence for id " << id1 << " and " << id2 << " successfully!" << endl;
}
// 去除指定 id 对应的序列中的重复元素
void uniqueSeq(int id) {
auto it = find(idSet.begin(), idSet.end(), id);
if (it == idSet.end()) {
cout << "id " << id << " does not exist!" << endl;
return;
}
auto& seq = seqs[it - idSet.begin()];
unordered_set<int> numSet;
seq.erase(remove_if(seq.begin(), seq.end(), [&numSet](int num) {
if (numSet.find(num) != numSet.end()) {
return true;
}
numSet.insert(num);
return false;
}), seq.end());
cout << "remove duplicates from sequence for id " << id << " successfully!" << endl;
}
// 输出指定 id 对应的序列中的元素
void outputSeq(int id) {
auto it = find(idSet.begin(), idSet.end(), id);
if (it == idSet.end()) {
cout << "id " << id << " does not exist!" << endl;
return;
}
auto& seq = seqs[it - idSet.begin()];
sort(seq.begin(), seq.end());
for (int num : seq) {
cout << num << " ";
}
cout << endl;
}
int main() {
while (true) {
string cmd;
cin >> cmd;
if (cmd == "new") {
int id;
cin >> id;
createSeq(id);
} else if (cmd == "add") {
int id, num;
cin >> id >> num;
addNum(id, num);
} else if (cmd == "merge") {
int id1, id2;
cin >> id1 >> id2;
mergeSeq(id1, id2);
} else if (cmd == "unique") {
int id;
cin >> id;
uniqueSeq(id);
} else if (cmd == "out") {
int id;
cin >> id;
outputSeq(id);
} else {
cout << "invalid command!" << endl;
}
}
return 0;
}
```
该程序使用了一个 `unordered_set` 存储已经使用的 id,使用一个 `vector` 存储每个 id 对应的序列。对于每个指令,程序会根据指令类型调用相应的函数实现相应的功能。
阅读全文