存储在向量中的类的计数器和ID属性

发布于 2025-02-02 03:16:04 字数 635 浏览 4 评论 0原文

关于删除数据库功能代码,我有一个困境。

每当我以唯一删除向量中的数据库时,我都不会想写可以用删除数字填充空白的代码(就像我删除了数据库ID3一样,我希望其他数据库的ID会增加稳定的序列,因此带有ID4的数据库将成为ID3)。

我也不知道如何减少我的静态插件。

文件:

**void Database::Rem()
{
    int dddddet;
    
    cin >> dddddet;
    
    if (iter !=  DbMain.end())
    {
        DbMain.erase(iter);
    }
}**
std::istream &operator>>(std::istream &re, Base &product)
{
    
}

}
std::ostream &printnames(std::ostream &pr, Base &pro)
{
    pr << "\nID:" << pro.ID << "\nName:" << pro.name;
    return pr;
}

标头文件:


"




I have a dilemma regarding my removing database function code.

Whenever I remove the database in vector with unique, I cannot think of writing the code that could fill the gap with the removed number (like I removed database ID3 and I want that the IDs of further databases would increment to have a stable sequence, so the database with ID4 would become ID3).

I also don't know how to decrement my static int counter.

File:

**void Database::Rem()
{
    int dddddet;
    
    cin >> dddddet;
    
    if (iter !=  DbMain.end())
    {
        DbMain.erase(iter);
    }
}**
std::istream &operator>>(std::istream &re, Base &product)
{
    
}

}
std::ostream &printnames(std::ostream &pr, Base &pro)
{
    pr << "\nID:" << pro.ID << "\nName:" << pro.name;
    return pr;
}

Header file:


"




如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巾帼英雄 2025-02-09 03:16:04

您在这里做的事情称为设计 anti tanternn :在很多情况下您很容易提出的一些结构性思想,但这将是很多麻烦(即,坏的)。它被称为a singleton :您假设只有一个dbmain,所以您将其长度存储在“类似全球”的静态成员中。没有道理!只需使用dbmain的长度即可。无论如何,您绝对不应需要全球或静态成员来计算您集中存储的对象。

您实际上永远不需要 ID成为您存储的对象的一部分 - 它的总体目的是dbmain存储中的索引。向量已经订购了!因此,只需打印向量中的位置,而不是在迭代矢量时打印.ID。而不是“找到具有id == n和擦除的基础”,您可以执行“擦除(dbmain.begin() + n)元素”,然后使用它。

The thing you're doing here is called a design anti-pattern: Some structural idea that you could easily come up with in a lot of situations, but that's going to be a lot of trouble (i.e., bad). It's called a singleton: You're assuming there's only ever going to be one DbMain, so you store the length of that in a "global-alike" static member. Makes no sense! Simply use the length of DbMain. You should never need a global or a static member to count objects that you store centrally, anyways.

You never actually need the ID to be part of the object you store – its whole purpose is being the index within the dBMain storage. A vector is already ordered! So, instead of printing your .ID when you iterate through the vector, simply print the position in the vector. Instead of "find the base with ID == N and erase" you could do "erase the (DbMain.begin() + N) element" and be done with it.

み青杉依旧 2025-02-09 03:16:04

设计中的问题在于,当您使用静态计数器初始化ID时,您似乎将唯一ID与向量中的索引相关联。这不是一个好主意,因为:

  • 您需要在每个删除处重新录制许多ID,这将使维护交叉引用非常困难。
  • 您可以拥有几个具有自己的ID集的数据库。
  • 如果您在不阅读基础的情况下添加基础,就有可能您不会符合适当价值的风险。

此外,迭代依次寻找ID并不是很有效。一种更有效的方法是将基础存储在std :: map:这允许允许非常有效地找到ID,通过ID索引而不是顺序编号。

然后,您唯一关心的是确保ID的唯一性。当然,如果您确保在创建新的基础时确保将其更新,并且该状态在文本文件中保存所有这些。您只需要清楚地表明,ID不能保证是顺序的。确保有助于这种理解的务实方法是,如果尝试删除未找到的记录,则会发出错误消息。

The problem in your design is that you seem somehow to associate the unique ID with the index in the vector, as you initialize the IDs with a static counter. This is not a good idea, since:

  • you would need to renumber a lot of IDs at each delete and this would make it very difficult to maintain cross-references.
  • you could have several databases each with its own set of ids.
  • there is a risk that you'd not get the counter to a proper value, if you add bases without reading them.

Moreover, iterating sequentially looking for an ID is not very efficient. A more efficient approach would be to store your bases in a std::map: this allows to find IDs very efficiently, indexing by ID instead of sequential number.

Your only concern would then be to ensure uniqueness of IDs. Of course, your counter approach could work, if you make sure that it is updated whenever a new base is created, and that the state is persisted with the database in the text file in which you'll save all this. You just have to make clear that the IDs are not guaranteed to be sequential. A pragmatic way to ensure contribute to this understanding is to issue an error message if there is an attempt to delete a record that was not found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文