删掉一个之后还剩几条
运行下面这段程序(链地址法,五个人):
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using Row = pair<int, string>;
using Table = vector<vector<Row>>; // 每一格挂一串 (学号, 名字)
const vector<Row> FIVE = {{17, "阿岚"}, {24, "小满"}, {15, "阿泰"}, {13, "南风"}, {23, "北辰"}};
void del_key(Table& t, int key) {
vector<Row>& cell = t[key % t.size()];
for (size_t i = 0; i < cell.size(); i++) {
if (cell[i].first == key) {
cell.erase(cell.begin() + i);
return;
}
}
}
int main() {
Table table(8);
for (const Row& r : FIVE) table[r.first % 8].push_back(r);
del_key(table, 15);
size_t n = 0;
for (const auto& cell : table) n += cell.size();
cout << n << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)
全部评论