一个集合是否可能包含两个指向同一对象的共享指针?

发布于 2024-12-08 12:50:56 字数 1553 浏览 1 评论 0 原文

在我的代码中,我有两个向量:

vector<lipid*> lipids;
vector<shared_ptr<bead> > ions;

脂质类:

class lipid{
 public:
  lipid();
  lipid(double x, double y, bool up, int LID);
  ~lipid();
  void makeMe(int LID);
  std::tr1::shared_ptr<bead> head;
  std::tr1::shared_ptr<bead> body;
  std::tr1::shared_ptr<bead> tail;
  int LID;
  vec direction;
};

我在脂质的构造函数中生成头、体、尾珠。

std::tr1::shared_ptr<bead> he(new bead);
std::tr1::shared_ptr<bead> bo(new bead);
std::tr1::shared_ptr<bead> ta(new bead);
this->head = he;
this->body = bo;
this->tail = ta;

一些脂质头通过以下方式插入离子矢量:

vector<lipid*>::iterator lit = lipids.begin();
while (lit != lipids.end()){
  lipid * l = *lit++;
  if (l->head->charge != 0) this->ions.push_back(l->head);
}

其中电荷是珠子的整数属性。 我还有一个map、set; > > 存储一些名为 box 的珠子。要将 bead 添加到我使用的任何地图值中:

bead b = l->head; //b white also be accessed through a beads vector that holds a shared pointer to all beads in the simulation
vector<int> t = b->getBox(); //a function that returns a valid box
boxes[t].insert(b);

在特定部分中,我有一组名为 cBeads 的共享指针,并将珠子插入两个单独的珠子中循环;第一个检查一些框并将其插入 cBeads 设置任何已更改 bean 的珠子,另一个检查所有离子并将它们插入。

我知道共享指针应该是指针或其他东西的所有者,这是否意味着我可以放置两个共享指针,指向集合中的同一个对象?

我希望这些对你来说有意义。

In my code I have two vectors:

vector<lipid*> lipids;
vector<shared_ptr<bead> > ions;

The lipid class:

class lipid{
 public:
  lipid();
  lipid(double x, double y, bool up, int LID);
  ~lipid();
  void makeMe(int LID);
  std::tr1::shared_ptr<bead> head;
  std::tr1::shared_ptr<bead> body;
  std::tr1::shared_ptr<bead> tail;
  int LID;
  vec direction;
};

And I generate the head, body, tail beads in the constructor of the lipid.

std::tr1::shared_ptr<bead> he(new bead);
std::tr1::shared_ptr<bead> bo(new bead);
std::tr1::shared_ptr<bead> ta(new bead);
this->head = he;
this->body = bo;
this->tail = ta;

Some of the lipids heads are inserted to the ions vector by:

vector<lipid*>::iterator lit = lipids.begin();
while (lit != lipids.end()){
  lipid * l = *lit++;
  if (l->head->charge != 0) this->ions.push_back(l->head);
}

where charge is an integer property of the bead.
I also have a map<vector<int>, set<shared_ptr<bead> > > to store some of the beads named boxes. To add a bead into any of the map values I use:

bead b = l->head; //b white also be accessed through a beads vector that holds a shared pointer to all beads in the simulation
vector<int> t = b->getBox(); //a function that returns a valid box
boxes[t].insert(b);

In a specific part I have a set of shared pointer named cBeads and I insert to it beads in two, separate loops; the first goes over some of the boxes and insert into the cBeads set any bead that has bean changed and the other goes over all the ions and insert them.

I know that that a shared pointer is supposed to be the owner of the pointer or something, does this mean that I can place two shared pointer, pointing to the same object in a set?

I hope any of this make sense to you.

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

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

发布评论

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

评论(4

东走西顾 2024-12-15 12:50:56

不,这是不可能的。

std::set 保证它不包含重复项。一个实例是否是另一个实例的重复项是通过应用第二个模板参数来决定的,该参数默认为 std::less 。

标准库提供了一个运算符<,它对std::shared_ptr进行操作,对底层指针执行小于比较。

No, it isn't possible.

std::set guarantees that it containes no duplicates. Whether an instance is a duplicate of another is decided by applying the second template argument, which defaults to std::less.

The standard library provides an operator < that operates on std::shared_ptr, performing a less than comparrison on the underlying pointers.

回眸一笑 2024-12-15 12:50:56

请参阅 http://www.boost.org/doc/ libs/1_47_0/libs/smart_ptr/shared_ptr.htm#comparison(或 TR1,或 C++11 标准)。

如果两个 shared_ptr 指向同一个对象,那么它们在 operator< 下比较为等价的,因此它们在 std::set 范围内是重复的。珠子> > 关注。

Consult http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/shared_ptr.htm#comparison (or TR1, or the C++11 standard).

If two shared_ptr point to the same object, then they compare as equivalent under operator<, so they're duplicates as far as std::set<shared_ptr<bead> > is concerned.

花间憩 2024-12-15 12:50:56

您使用的比较功能是什么?如果是默认值(std::less),则 std::set 中不能有两个指向同一对象的指针;就此而言,我想不出一个可能的比较函数可以满足要求并允许它。

What is the comparison function you are using? If it is the default (std::less), then you cannot have two pointers to the same object in the std::set; for that matter, I can't think of a possible comparison function which meets the requirements, and would allow it.

别靠近我心 2024-12-15 12:50:56

一般来说,将共享指针视为标准指针,因为这通常(我说通常是保守的)共享指针类背后的目标。

此外,共享指针背后的概念绝对不是标准指针的“所有者”,而是相反,即:在多个参与者之间共享所有权。

两个不同的共享指针实例(内部具有相同的指针)应该(将)比较相等,任何其他行为都将非常违反直觉。

如果使用 std::set 来限制唯一条目,那么它应该按预期执行,但是如果您期望重复项,则需要另一个容器,例如 std::vector。

希望这有帮助

In general, think of a shared pointer as if it was a standard pointer as this is usually (I say usually to be conservative) the goal behind the shared pointer class.

Also, the notion behind shared pointer is definitely not about being the “owner” of a standard pointer but the opposite, ie: sharing the ownership amongst multiple participants.

Two different shared pointer instances (with the same pointee inside) should (will) compare equal, any other behaviour would be extremely counter intuitive.

If the use of the std::set was to restrict to unique entries, then it should perform as expected, however if you expect duplicates, you will need another container like a std::vector.

Hope this help

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