2d array with different types of elements and using classes
这里的故事和其他人的故事一样。对c来说是个新手(通常是编程人员),并且认真地执着于分配。
我必须为DVD商店创建并实现一个类。我还必须创建一个数组来存储有关DVD的信息。数组的元素必须是类类型(必须创建)
到目前为止,我的挑战是数组必须包含int,string类型的元素。据我所知(很多谷歌搜索),这是不可能的。
我觉得使用类可以某种方式实现,但是我找不到解决方法。
请帮助。
谢谢
利昂
顺便说一句,您正在使用原始数据库,因此让我们将DVD存储项目称为记录。这个想法是在需要特定细节之前,尽可能通用地对待事物。
DVD存储记录:
1 2 3 4 5 6 7 | struct Record { std::string title; std::string artist; std::string genre; double price; }; |
现在要创建一个数据库来保存所有记录:
1 | std::vector<Record> database; |
您可能希望创建索引表来帮助加快搜索速度,而不必每次都诉诸数据库。
1 2 | std::map<std::string /* Title */, unsigned int /* Index into database */> Index_By_Title; std::map<std::string, unsigned int> Index_By_Artist; |
这将成为2d容器,因为您正在通过使用向量的索引来访问行,然后可以使用成员名称来访问列。