STL 向量和指向向量元素的指针

发布于 2024-12-09 01:29:22 字数 438 浏览 0 评论 0原文

有两种 STL 向量,第一个包含对象,第二个包含指向对象的指针。

v_objects = [obj1, obj2, obj3, obj4]
v_ptr = [ptr_to_obj1, ptr_to_obj2, ptr_to_obj3, ptr_to_obj4]

向量 v_ptr 用于对 v_objects 中的元素进行排序。假设向 v_objects 添加了一些内容,如下所示:

v_objects = [obj1, obj2, obj3, obj4, obj5]

假设 v_objects 在插入后被重新分配到其他位置,并且 v_ptr 中的指针无效。现在我想使用 v_ptr 中的指针对对象进行排序,但它们无效。是否可以以某种方式创建一些聪明的指针,这些指针指向与重新分配之前相同的对象(使用 stl::vector)?

There are two STL vectors, one with objects and second with pointers to objects.

v_objects = [obj1, obj2, obj3, obj4]
v_ptr = [ptr_to_obj1, ptr_to_obj2, ptr_to_obj3, ptr_to_obj4]

Vector v_ptr is used to sort elements in v_objects. Let's say that something is added to v_objects so it looks like:

v_objects = [obj1, obj2, obj3, obj4, obj5]

Let's say that v_objects are after insertion reallocated somewhere else and pointers in v_ptr are invalid. Now I want to sort objects using their pointers in v_ptr, but they are invalid. Is it somehow possible to create some clever pointers which point to the same object as they were before reallocation (using stl::vector)?

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

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

发布评论

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

评论(3

江湖彼岸 2024-12-16 01:29:22

我不这么认为。您可以通过使用带有 < 的单个向量来省去麻烦。 code>shared_ptr 到你的对象,或者只使用 ptr_vector

I don't think so. You could probably save yourself the trouble by just using a single vector with shared_ptrs to your objects, or just use a ptr_vector.

狼性发作 2024-12-16 01:29:22

有几种方法可以解决这个问题:

  1. 您可以更改v_objects来存储指向对象的指针(这将使内存管理复杂化,但使用智能指针可能会有所帮助)。
  2. 您可以更改 v_ptr 以将索引存储到 v_objects 而不是指针。

我个人的偏好是后者。

There are several ways to tackle this:

  1. You could change v_objects to store pointers to objects (this will complicate memory management, but using smart pointers might help).
  2. You could change v_ptr to store indices into v_objects instead of pointers.

My personal preference would be the latter.

神也荒唐 2024-12-16 01:29:22

我不完全理解你想要做什么,但你可以将索引存储到容器中,而不是指针。用于排序的比较器函子将存储对向量的引用并取消引用元素以获得值。

只要原始向量中元素的顺序不改变(即仅在末尾插入),索引向量仍然有效。如何处理新添加的元素,或者是否对原始向量执行任何其他更改是另一回事:

// sketch (reorganize code as needed):
std::vector< type > original = load();
struct compare_indexed {
   std::vector< type > const & v;
   compare_indexed( std::vector< type > const & v ) : v(v) {}
   bool operator()( int lhs, int rhs ) const {
      return v[lhs] < v[rhs];
   }
};
// Create original index vector
std::vector< int > indices;
for ( unsingned int i = 0; i < original.size(); ++i ) {
   indices.push_back( i );
}
std::sort( indices.begin(), indices.end(), compare_indexed( original ) );

I don't fully understand what you want to do, but you could store indices into the container, rather than pointers. The comparator functor for the sorting would store a reference to the vector and dereference the elements to obtain the values.

As long as the order of the elements in the original vector does not change (i.e. only insertions at the end) the vector of indices would still be valid. What to do with the newly added elements, or if any other change is performed on the original vector is a different story:

// sketch (reorganize code as needed):
std::vector< type > original = load();
struct compare_indexed {
   std::vector< type > const & v;
   compare_indexed( std::vector< type > const & v ) : v(v) {}
   bool operator()( int lhs, int rhs ) const {
      return v[lhs] < v[rhs];
   }
};
// Create original index vector
std::vector< int > indices;
for ( unsingned int i = 0; i < original.size(); ++i ) {
   indices.push_back( i );
}
std::sort( indices.begin(), indices.end(), compare_indexed( original ) );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文