简体   繁体   English

如何保持指向存储在向量中的结构的指针有效?

[英]How to keep pointers to structures stored in vector valid?

I am learning C++ and I have trouble with pointers to structures stored in vector.我正在学习 C++,但在指向存储在向量中的结构的指针时遇到了麻烦。 The problem is that I need to keep the structure Student sorted twice.问题是我需要保持结构 Student 排序两次。 Once by student's id and another time by student's name, so it is easy to search the values in it.一次是学生的id,另一次是学生的名字,所以很容易搜索其中的值。 Because of this I created two vectors of pointers:因此,我创建了两个指针向量:

vector<Student *> sortedByID;    
vector<Student *> sortedByName;

The structure looks like this and I keep it in vector as well (even though it is probably not a good idea):结构看起来像这样,我也将它保存在向量中(尽管这可能不是一个好主意):

struct Student {
    int id;
    string name;
};

vector <Student> students;

I am creating the new struct with push_back and filling it with parameters from a function (yes, I have a constructor).我正在使用 push_back 创建新结构并用函数中的参数填充它(是的,我有一个构造函数)。 To keep the vector of pointers sorted I am using lower_bound as shown below:为了保持指针向量排序,我使用了lower_bound,如下所示:

students.push_back(Student(id, name));
it = lower_bound(sortedByID.begin(), sortedByID.end(), id, cmp());
sortedByID.insert(it, &(students.back()));
//the same for name

The problem is that everytime I add the structure with push_back it reallocs new vector and destroys the address of previous objects, so pointers in vector sortedByID point to invalid value.问题是每次我用 push_back 添加结构时,它都会重新分配新向量并破坏先前对象的地址,因此向量 sortedByID 中的指针指向无效值。 I think it would be the same with an array of structs, because once the array is full, there is no other way (as far as I know) to resize it, than to create a new array and copy all the data from previous one (so the address will change again).我认为它与结构数组相同,因为一旦数组已满,就没有其他方法(据我所知)来调整它的大小,而不是创建一个新数组并复制前一个数组中的所有数据(因此地址将再次更改)。

Is there any clever way to solve this problem?有什么聪明的方法可以解决这个问题吗? Please note that I am only allowed to use vector and not any other containers from STL.请注意,我只被允许使用 vector 而不是来自 STL 的任何其他容器。

There are three options to solve this issue using only vectors and no other containers:仅使用向量而不使用其他容器可以通过三个选项来解决此问题:

1) Avoid the reallocation. 1) 避免重新分配。 This can only be achieved if you know the maximum M of expected number of elements to be inserted in your vector.这只有在您知道要插入向量中的预期元素数量的最大 M 时才能实现。 In this case you canstudents.reserve(M);在这种情况下,您可以students.reserve(M); . .

2) Forget the the pointers for sortedByID and sortedByName . 2) 忘记sortedByIDsortedByName的指针。 Use integers (or better said size_t ) store the index of a student in students instead of a pointer.使用整数(或更好说size_t )存储在一个学生的指数students ,而不是一个指针。 This supposes of course that the order of items in students is never changed.这当然假设学生中的项目顺序永远不会改变。

3) Do not store the students themselves in a vector, but make students a vector of pointers to students (unsorted) that are allocated from free store. 3)不要将学生本身存储在向量中,而是让students成为从自由存储分配的指向学生(未排序)的指针的向量。 If this alternative meets all your criteria, i'd suggest to go fore shared_ptr<Student> instead of raw pointers.如果此替代方案符合您的所有标准,我建议您使用shared_ptr<Student>而不是原始指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM