在我的代码中,我有两个向量:
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.
发布评论
评论(4)
不,这是不可能的。
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 tostd::less
.The standard library provides an
operator <
that operates onstd::shared_ptr
, performing a less than comparrison on the underlying pointers.请参阅 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 underoperator<
, so they're duplicates as far asstd::set<shared_ptr<bead> >
is concerned.您使用的比较功能是什么?如果是默认值(
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 thestd::set
; for that matter, I can't think of a possible comparison function which meets the requirements, and would allow it.一般来说,将共享指针视为标准指针,因为这通常(我说通常是保守的)共享指针类背后的目标。
此外,共享指针背后的概念绝对不是标准指针的“所有者”,而是相反,即:在多个参与者之间共享所有权。
两个不同的共享指针实例(内部具有相同的指针)应该(将)比较相等,任何其他行为都将非常违反直觉。
如果使用 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