在我的代码中,我有两个向量:
vector<lipid*> lipids;
vector<shared_ptr<bead> > ions;
lipid
类:
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);
}
其中charge
是bead
的整数属性。我还有一个map<vector<int>, set<shared_ptr<bead> > >
来存储一些名为boxes的珠子。要将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
集合中,另一个循环遍历所有离子并插入它们。
我知道共享指针应该是指针或其他东西的所有者,这是否意味着我可以放置两个共享指针,指向一个集合中的同一对象?
我希望这一切对你有任何意义。
发布于 2011-10-05 18:20:59
不,这不可能。
std::set
保证它不包含重复项。一个实例是否与另一个实例重复是通过应用第二个模板参数来决定的,该参数默认为std::less
。
标准库提供了一个在std::shared_ptr
上运行的operator <
,在底层指针上执行小于比较。
发布于 2011-10-05 18:20:15
请参考http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/shared_ptr.htm#comparison (或TR1,或C++11标准)。
如果两个shared_ptr
指向同一个对象,那么它们在operator<
下是等价的,所以就std::set<shared_ptr<bead> >
而言,它们是重复的。
发布于 2011-10-05 18:19:45
您使用的比较函数是什么?如果它是缺省的(std::less
),那么在std::set
中不能有两个指向同一个对象的指针;在这个问题上,我想不出一个可能的比较函数来满足要求,并且允许这样做。
https://stackoverflow.com/questions/7659676
复制相似问题