gpt4 book ai didi

c++ - 如何有效地设计多键查找,其中一个键是C++中元素的 vector

转载 作者:行者123 更新时间:2023-12-02 10:08:51 25 4
gpt4 key购买 nike

这个问题更适合于如何有效地设计此解决方案:
与我要解决的问题类似,请考虑一下我有与每个类(class)相关的学生列表。

类(class)A:学生名单_1

类(class)B:学生名单_2
....

笔记:

->同一学生可以与多个类(class)相关联。

->学生名单经常变化

现在,我应该在此处执行2个操作:

一种)。具有发布特定类(class)公告的功能:在这种情况下,我需要该类(class)的在册学生名单。

b)。具有查询学生注册的所有类(class)的功能。

我研究了增强型多索引容器,但它们不能解决问题:我的数据不是可以根据键排序的“类(class):学生”;如果每门类(class)都查看它,则它更像“course:list_of_students”。

最接近的设计模式(我对设计模式非常陌生)是观察者模式,但它更多的是一种单向方式:为了有效解决此问题,我将必须使用两种方式的观察者模式。

现在,存在一个存在类似问题的线程:Data structure: Is there something like a two way observer pattern?
关于它的答案不是很令人满意。我不确定恢复一个已有旧的线程或启动一个新线程是一个好主意,所以我选择了后者。

任何帮助将得到真诚和真诚的感谢。提前致谢。

最佳答案

//   Sorted (any deterministic comparison) random access 
// collections of student and course descriptors/records
//
// Insertion via binary search (SO search "c++ insert to sorted vector)

vector<student> students;
using students_t = decltype(students);

vector<course> courses;
using courses_t = decltype(courses);

// bidirectional student to course relation map
boost::bimap::bimap<
multiset<students_t::size_type>,
multiset<courses_t::size_type>
> assignments;
using assignments_t = decltype(assignments);

要么
// record must be inserted to students_t/courses_t before its enrollments_/subscriptions_ can be populated

struct enrolled_student {
student student_;
set<courses_t::size_type> enrollments_;
}

struct subscribed_course {
course course_;
set<students_t::size_type> subscriptions_;
}

vector<enrolled_student> students;
using students_t = decltype(students);

vector<subscribed_course> courses;
using courses_t = decltype(courses);

关于c++ - 如何有效地设计多键查找,其中一个键是C++中元素的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26115607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com