删除矢量数据,不要向前推进

Delete vector data and not to push forward

本文关键字:数据 删除      更新时间:2023-10-16

我有一个向量,它的数据为

战绩1:03:泰德
记录2:06:玛丽
战绩3:23:渡轮

如何删除Record 2,以使Record 3数据不会向上推送并最终变得Record 2

如果我理解正确,请使用记录编号到记录的映射:

std::unordered_map<std::size_t, Record> records{ //or std::map in C++03
    {1, {"03", "Ted"}}, //or records[1] = Record("03", "Ted") in C++03
    {2, {"06", "Mary"}}, //or records[2] = Record("06", "Mary") in C++03
    {3, {"23", "Ferry"}} //or records[3] = Record("23", "Ferry") in C++03
};
records.erase(2); //leaves records[1] and records[3] intact

有关完整的演示,请参阅此处 fpr C++11,或此处的 C++03,尽管 C++03 可以通过 insert 进行改进,因此不需要Record的默认构造函数。我想 C++11 在迭代时也可以使用 const,尽管这无关紧要。